【问题标题】:Radio button value not updating in PHP单选按钮值未在 PHP 中更新
【发布时间】:2014-09-01 17:54:01
【问题描述】:

我的表单中有 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 中我回显 $_POST['type'] ,我总是得到在页面加载期间使用 jquery 设置的值,即在这种情况下是 b 而不是 d。

为什么值没有更新?

谢谢。

更新:谢谢大家,这是由于在单选按钮上调用了无意的 val()。因此,如果使用 val() 设置单选按钮值,它以后不会改变,奇怪的行为。

【问题讨论】:

  • 但那是在加载期间,然后我在表单提交之前单击并将单选按钮值设置为 d
  • 在这种情况下,当有提交操作时,将页面加载值更改为d(或提交之前选择的任何内容)而不是默认的b
  • 代码的其他部分是否也选择了b option
  • @tradyblix 在用户单击单选按钮后,我是否需要使用 jquery 手动设置选定的单选按钮
  • @tastro no ,只选择一次

标签: php jquery html


【解决方案1】:

在 jQuery 1.6+ 中

$('#b').prop('checked', true);
$('#b').prop('checked', false);

jQuery 1.5 及以下

$('#b').attr('checked','checked');
$('#b').removeAttr('checked');

【讨论】:

  • 如果用户单击其他单选按钮,我是否需要手动删除选中的属性?
  • 不,您不必这样做。这些按钮按 name='type' 分组。
  • 谢谢它是由于无意的 val() 调用单选按钮,不确定 -1,upvoted :)
【解决方案2】:

而不是使用

$("#b").prop("checked", true);

你为什么不把你的单选按钮写成

<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

【讨论】:

  • 因为我正在进行 ajax 调用 [当用户在表单中输入第一个字段时] 以确定需要选择哪个单选按钮。这是一个更新页面,一旦用户在表单的第一个字段中输入他的 ID,他的其他详细信息就会填充到要编辑和提交的表单中。
【解决方案3】:

像魅力一样工作;-))。

<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>

【讨论】:

  • 谢谢,它可以正常工作,但在我的情况下不起作用,可能与在 ajax 调用中设置单选按钮值有关。
  • @ak111in 你能展示更多你的代码吗?您的浏览器是否正确更新了收音机的选择?
【解决方案4】:

试试这个代码

<?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>

【讨论】:

    【解决方案5】:

    试试:

     $("#b").attr("checked", true);
    

         $("#b").attr("checked", "checked");
    

    【讨论】:

    • 没有任何区别
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多