根据查询字符串选择单选项


Selecting Radio Option based on Query String

我在选择与查询字符串关联的无线电选项时遇到了一些麻烦。我有下面的代码…

<?php if ( $_GET['fp'] == 'floorplanfive' ) { ?>
  <script type="text/javascript">
    jQuery('#choice_2_4').attr('checked', 'checked');
    console.log("test");
  </script>
<?php } ?>

控制台显示消息'test',但没有选择无线电选项。

该页面可在此处找到。

请帮忙。: -)

您需要将代码放入$(document).ready()函数中:

jQuery(document).ready(function() {
  // your code
});

使用

jQuery('#choice_2_4').prop('checked', true);

编辑:如果您的单选按钮在此代码之后加载,则使用它:

jQuery(document).ready(function () {
    jQuery('#choice_2_4').prop('checked', true);
}

使用==比较运算符代替=赋值。

if条件下的赋值总是返回true。

把你的代码改成:

<?php if ( $_GET['fp'] == 'floorplanfive' ) { ?>
  <script type="text/javascript">
    jQuery('#choice_2_4').attr('checked', 'checked');
    console.log("test");
  </script>
<?php } ?>

尝试:

jQuery('#choice_2_4').attr('checked', true).checkboxradio("refresh");
相关文章: