简体   繁体   English

jQuery查找具有特定值的所有元素

[英]jquery find all elements having a certain value

When a user clicks on my radio button: 当用户单击我的单选按钮时:

<input type="radio" name="allselect" onClick="javascript: _select('none');" />

I need the function _select(param) to find every radio button with a value of param, so 'none' in this case, and click it. 我需要函数_select(param)来查找每个具有param值的单选按钮,因此在这种情况下为“ none”,然后单击它。 How can this be done in jquery? 如何在jquery中完成?

Since you are using jQuery: 由于使用jQuery:

<input type="radio" name="allselect" id="selectnone" />

JS: JS:

$('#selectnone').click(function(){
    $('input[type=checkbox]').each(function(){
          if(this.value === 'none') this.checked = true;
    });
});

I'd think that the following, as yet untested approach, would work: 我认为以下尚未通过测试的方法会起作用:

$('input:radio').click(
    function(){
        var inputsOfValueEqualToNone = $('input:radio[value="none"]');
    });

Making the above into a more function-based approach: 使以上内容成为基于功能的方法:

function _select(param){
    if (!param){
        return false;
    }
    else {
        inputs = $('input:radio[value="' + param + '"]').click();
    }
}

References: 参考文献:

Try this 尝试这个

function _select(param){
   var rBtns = $('input[name=' + param + ']');
   rBtns.click();//Calling the click method on all the radio buttons it found
}

You can do this: 你可以这样做:

function _select(paramVal){
   $('input[value=' +paramVal +']').trigger('click');
}

Use: 采用:

function _select( value ) {
  var elems $('input:radio[value='+value+']'); // all the radio buttons with the value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM