如何单选按钮更改表单操作地址

use*_*711 2 html javascript php forms jquery

如何使单选按钮更改表单操作地址

我有一张表格,内容如下

和一个单选按钮

<b>Would you like to to make payment ? <input type="radio" name="choice" value="yes">Yes <input type="radio" name="choice" value="no" checked>No</b>'
Run Code Online (Sandbox Code Playgroud)

如果用户选择no(默认选中),则表单action仍为register_page4.php

但如果用户选择yes并按下提交按钮:

<input  id="btnSubmit" type="submit" value="Next" />
Run Code Online (Sandbox Code Playgroud)

我希望表单操作是payment.php而不是register_page4.php,我该如何实现它.

我进行了更改,这就是我输入的内容

<html>
<head>
</head>
<body>

  <form name="form1" method="post" action="register_page4.php">

    Would you like to make an appointment for collection ? 
<input type="radio" name="collection" value="yes">Yes 
<input type="radio" name="collection" value="no" checked>No
   <input  id="btnSubmit" type="submit" value="Next" />  
    </form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
jQuery(document).ready(function($) {
    var form = $('form[name="form1"]'),
        radio = $('input[name="choice"]'),
        choice = '';

    radio.change(function(e) {
        choice = this.value;

        if (choice === 'yes') {
            form.attr('action', 'payment.php');
        } else {
            form.attr('action', 'register_page4.php');
        }
    });
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

但结果仍然是register_page4.php,即使我单击单选按钮,我尝试点击两者,两者仍然转到register_page4.php

Mik*_*ckx 6

这是一个使用JavaScript解决方案的示例.基本上,当更改单选按钮时,表单的属性(此处带有id #yourForm)会以正确的操作进行更改.

jQuery(document).ready(function($) {
    var form = $('form[name="form1"]'),
        radio = $('input[name="collection"]'),
        choice = '';

    radio.change(function(e) {
        choice = this.value;

        if (choice === 'yes') {
            form.attr('action', 'payment.php');
        } else {
            form.attr('action', 'register_page4.php');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)