HTML,制作一个按钮将数据提交给另一个网址?

10 html

以下是我的HTML:

<form class="form-horizontal" method="post">
        <div class="control-group">
            <label class="control-label" for="inputEmail">Email</label>
            <div class="controls">
                <input type="text" id="inputEmail" placeholder="Email">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputPassword">Password</label>
            <div class="controls">
                <input type="password" id="inputPassword" placeholder="Password">
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <label class="checkbox">
                    <input type="checkbox"> Remember me
                </label>
                <button type="submit" class="btn btn-success">Sign in</button>
                <button class="btn btn-primary"> Make a new account </button>
            </div>
        </div>
    </form>
Run Code Online (Sandbox Code Playgroud)

注意:万一有人想知道,它正在使用bootstrap库.

我有表格,它POST到login.php但我希望按钮Make a new account相同的数据发布到register.php.

那可能吗?如果是这样,怎么样?

fil*_*pko 11

将标记的action属性设置form为要将数据发送到的页面的URL.


Viv*_*ain 9

请尝试以下方法:

在HTML中为Make a new account按钮添加onclick事件处理程序:

<form class="form-horizontal" method="post" id="myform">
        <div class="control-group">
            <label class="control-label" for="inputEmail">Email</label>
            <div class="controls">
                <input type="text" id="inputEmail" placeholder="Email">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="inputPassword">Password</label>
            <div class="controls">
                <input type="password" id="inputPassword" placeholder="Password">
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <label class="checkbox">
                    <input type="checkbox"> Remember me
                </label>
                <button type="submit" class="btn btn-success">Sign in</button>
                <button class="btn btn-primary" onclick="javascript:register()"> Make a new account </button>
            </div>
        </div>
    </form>
Run Code Online (Sandbox Code Playgroud)

在JavaScript中:

function register() {
    this.action = 'register.php';
    return true;
}
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,您可以尝试:

function register() {
    var frm = document.getElementById("myform");
    frm.action = 'register.php';
    return true;
}
Run Code Online (Sandbox Code Playgroud)

上述解决方案中的一个或两个应该适合您.更喜欢第一种方法,因为它更清洁.

请将此作为起点,而不是复制粘贴解决方案,并遵循开发的最佳实践.

  • 为什么在HTML中存在完全存在的属性时使用JS? (2认同)
  • @ItayGal,表单上有两个提交按钮.提问者想要将相同的表单提交给两个不同的URL,具体取决于用户点击的按钮.所以,**我们使用一个按钮**的action属性的默认值**和点击第二个按钮**的**更新动作属性. (2认同)

CBr*_*roe 5

我有表格,它POST到login.php,但我想按钮创建一个新帐户发布相同的数据到register.php.

那可能吗?

在HTML5中,使用提交按钮上的formaction属性.

但浏览器支持可能还不是很好.可以使用Polyfil解决,该Polyfil会自动将点击处理程序附加到此类按钮,并action动态更改表单的属性.