只允许以两种形式检查一个单选按钮

时间:2014-11-24 13:44:39

标签: javascript jquery html forms

我有两个表单,每个表单都有一个单选按钮。如何修复以便在涉及多个表单时只能检查一个单选按钮?

以下是两种形式的基本部分:

<form name="form1" action="http://www.my-site.com/go-here.php" method="post">
  <input type="radio" name="payment" value="radio1" checked="checked" /> Use form1
</form>

<form name="form2" action="http://www.my-site.com/instead-go-here.php" method="post">
  <input type="radio" name="payment" value="radio2" /> Use form2
</form>

1 个答案:

答案 0 :(得分:1)

HTML:

<form name="form1" action="http://www.my-site.com/go-here.php" method="post">
  <input type="radio" name="payment" value="radio1" /> Use form1
</form>

<form name="form2" action="http://www.my-site.com/instead-go-here.php" method="post">
  <input type="radio" name="payment" value="radio2" /> Use form2
</form> 

JavaScript的:

$("[name=payment]").on("click",function(){

    $("[name=payment]").prop('checked', false)
    $(this).prop('checked', true)

});

小提琴:http://jsfiddle.net/44mycngy/