我的表单中有 4 个单选按钮:
<tr><td>Type</td><td>
<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D</td></tr>
在页面加载时,我使用 jquery 设置了一个单选按钮
$("#b").prop("checked", true);
现在我在表单中选择值 d 并提交。在 PHP 中,我 echo $_POST['type'] ,我总是使用 jquery 获取在页面加载期间设置的值,即在本例中为 b 而不是 d。
为什么该值没有更新?
谢谢。
更新:谢谢大家,这是由于无意中在单选按钮上调用了 val() 造成的。因此,如果使用 val() 设置单选按钮值,它以后不会改变,这是奇怪的行为。
最佳答案
在 jQuery 1.6+ 中
$('#b').prop('checked', true);
$('#b').prop('checked', false);
jQuery 1.5 及更低版本
$('#b').attr('checked','checked');
$('#b').removeAttr('checked');
关于php - PHP 中的单选按钮值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24692085/