jquery-打开并检查单选按钮onclick

时间:2016-12-16 08:12:43

标签: javascript jquery checkbox radio-button

我有一些单选按钮来过滤我网页上的数据,这些按钮对我来说非常好,但我无法检查该单选按钮......

<input id="radio1" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&scat=bank-manager'>
<label for="radio1">bank manager</label><br><br>

<input id="radio2" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&scat=ceo'>
<label for="radio2">ceo</label><br><br>

<input id="radio3" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&scat=company-head'>
<label for="radio3">company head</label>

我使用jquery打开并执行单击单选按钮的检查。

jquery的:

$(document).ready(function() {
    $('input[type=radio]').click(function() {
        window.location=$(this).attr('data-href');
    });
    // parse the url:
    var scat = window.location.search.match(/scat=(\w+)/)[1];
    if (typeof scat !== 'undefined') {
        // update the correct radio button:
        $('input[value="' + scat + '"]').prop("checked", true);
    }
});

如果我点击&#34; ceo&#34;使用上述查询单选按钮,它对我来说绝对没问题,即它为我过滤内容并执行检查... 另一方面,如果我点击银行经理或公司负责人。它只过滤内容..没有执行检查......任何帮助??

提前致谢...

注意: data-href包含有&#34; - &#34;在银行经理和公司负责人以及标签领域,银行经理和公司负责人都没有&#34; - &#34;。

3 个答案:

答案 0 :(得分:2)

您可以将&(您在网址中使用的错误符号,即&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp

您目前是,

data

添加<input id="radio1" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&scat=bank-manager'> <label for="radio1">bank manager</label><br><br> 属性以获取data-scat值,并且还可以根据scat点击

value

在jQuery中,您应该按如下方式访问它并附加到您的URL中,

<input id="radio1" type="radio" name="scat" value="bank-manager" data-scat="bank-manager" data-href='vlist.php?cat=xyz'>
<label for="radio1">bank manager</label><br><br>

答案 1 :(得分:0)

您的正则表达式需要修改为提取-,还需要将选择器更改为属性以选择器结尾

&#13;
&#13;
var search = "?cat=xyz&scat=bank-manager"; //replace with window.location.search
var scat = search.match(/scat=([\w-]+)/);
if (scat && scat[1]) {
  // update the correct radio button:
  $('input[data-href$=' + scat[1] + ']').prop("checked", true);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="radio1" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&scat=bank-manager'>
<label for="radio1">bank manager</label>
<br>
<br>

<input id="radio2" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&scat=ceo'>
<label for="radio2">ceo</label>
<br>
<br>

<input id="radio3" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&scat=company-head'>
<label for="radio3">company head</label>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你没有为你的无线电输入标签指定正确的值,你在jQuery选择器中使用了值。只需要为它添加正确的值,它就能正常工作。

应该是

<input id="radio1" type="radio" name="scat" value="bank-manager" data-href='vlist.php?cat=xyz&&subcat=bank-manager'>

insted of

<input id="radio1" type="radio" name="scat" value="1" data-href='vlist.php?cat=xyz&&subcat=bank-manager'>

$(document).ready(function() {
  $('input[type=radio]').click(function() {
    window.location = $(this).attr('data-href');
  });
  // parse the url:
  var search = "vlist.php?cat=xyz&subcat=company-head";
  // uncomment this line and comment below one  
  // var scat = window.location.search.match(/bcat=([\w-]+)/)[1];
  var scat = search.match(/bcat=([\w-]+)/)[1];
  if (typeof scat !== 'undefined') {
    // update the correct radio button:
    $('input[value="' + scat + '"]').prop("checked", true);
    
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="radio1" type="radio" name="scat" value="bank-manager" data-href='vlist.php?cat=xyz&&subcat=bank-manager'>
<label for="radio1">bank manager</label>
<br>
<br>

<input id="radio2" type="radio" name="scat" value="ceo" data-href='vlist.php?cat=xyz&&subcat=ceo'>
<label for="radio2">ceo</label>
<br>
<br>

<input id="radio3" type="radio" name="scat" value="company-head" data-href='vlist.php?cat=xyz&&subcat=company-head'>
<label for="radio3">company head</label>