[英]Radio button value not updating in PHP
I have 4 radio buttons in my form: 我的表格中有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>
On page load I set one of the radio buttons using jquery 在页面加载时,我使用jquery设置了一个单选按钮
$("#b").prop("checked", true);
Now I select the value d in my form and submit. 现在我在表单中选择值d并提交。 In PHP I echo $_POST['type'] , I am always getting the value which was set during page load using jquery ie in this case b instead of d. 在PHP中我回显$ _POST ['type'],我总是得到在页面加载期间使用jquery设置的值,即在这种情况下b而不是d。
Why is the value not updating? 为什么值不更新?
Thanks. 谢谢。
UPDATE:Thanks all, it was due to unintentional val() called on radio button. 更新:谢谢大家,这是由于无线电按钮上无意的val()调用。 So if radio button value is set using val() it will not change later, strange behavior. 因此,如果使用val()设置单选按钮值,则以后不会更改奇怪的行为。
In jQuery 1.6+ 在jQuery 1.6+中
$('#b').prop('checked', true);
$('#b').prop('checked', false);
jQuery 1.5 and below jQuery 1.5及以下版本
$('#b').attr('checked','checked');
$('#b').removeAttr('checked');
instead of using 而不是使用
$("#b").prop("checked", true);
why dont you write your radio buttons as 为什么不把你的收音机按钮写成
<input type="radio" name="type" id="a" value="a" >A
<input type="radio" name="type" id="b" value="b" checked="checked" >B
<input type="radio" name="type" id="c" value="c" >C
<input type="radio" name="type" id="d" value="d" >D
Works like a charm ;-)). 奇迹般有效 ;-))。
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js" /></script>
<script type="text/javascript">
$(document).ready(function() {
$('#b').attr('checked', 'checked');
});
</script>
</head>
<body>
<?php
if(isset($_POST['sbmt']) && isset($_POST['type'])) {
?>
<h1>Selected type: <?php echo($_POST['type']); ?></h1>
<?php
}
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
<ul>
<li><input type="radio" name="type" id="a" value="a" /> A</li>
<li><input type="radio" name="type" id="b" value="b" /> B</li>
<li><input type="radio" name="type" id="c" value="c" /> C</li>
<li><input type="radio" name="type" id="d" value="d" /> D</li>
</ul>
<input name="sbmt" type="submit" value="Submit" />
</form>
</body>
</html>
Try this code 试试这个代码
<?php
if(isset($_REQUEST['sb']))
{
echo $_REQUEST['type'];
}
?>
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(e) {
$('#b').attr('checked','checked');
});
</script>
<tr><td>Type</td><td>
<form name="frm" method="post" action="">
<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>
<input type="submit" name="sb" value="submit" />
</form>
</body>
</html>
Try: 尝试:
$("#b").attr("checked", true);
or 要么
$("#b").attr("checked", "checked");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.