Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery是否检测所选按钮的id?_Jquery_Button - Fatal编程技术网

Jquery是否检测所选按钮的id?

Jquery是否检测所选按钮的id?,jquery,button,Jquery,Button,嗨,这让我在圈子里转来转去 考虑以下单选按钮: <input type="radio" class="win" id="50" value="1" name="match1" /> <input type="radio" class="cover" id="50" value="2" name="match1" /> <input type="radio" class="win" id="51" value="1" name="match2" /> <

嗨,这让我在圈子里转来转去

考虑以下单选按钮:

<input type="radio" class="win" id="50" value="1" name="match1" />
<input type="radio" class="cover" id="50" value="2" name="match1" />

<input type="radio" class="win" id="51" value="1" name="match2" />
<input type="radio" class="cover" id="51" value="2" name="match2" />

<input type="radio" class="win" id="52" value="1" name="match3" />
<input type="radio" class="cover" id="52" value="2" name="match3" />

提交表单时,我想知道所选任何按钮的id

某些按钮对可能根本无法选择

我试过:

for (var x=1; x<4; ++x){

     if($('form #'+$(this).attr("id")+'input:radio[name=match'+x+']').prop('checked', true)){
          alert('Checked;);
     }

}

for(var x=1;x您正在将该值设置为true。从
prop
调用中删除
,true

if($('form #'+$(this).attr("id")+'input:radio[name=match'+x+']').prop('checked')){
      alert('Checked;);
}

当您使用
.prop('checked',true)
设置属性时,它返回您调用它的jQuery元素,并且该元素的计算结果为true。

您是
在if中设置选中的属性,而不仅仅是
获取

改变


我相信这会回答你的问题:

for (var i = 0, len = 4; i < len; i++) {
    if ( $('input[name=match' + i + ']:checked').val() ) {
        alert("Checked");
    }
}
for(变量i=0,len=4;i
以下是您的场景的完整工作代码,示例JSFIDLE位于:


函数alertChecked(){
对于(变量i=0,x=4;i
首先,不要使用多个ID,也不要以数字开头,因为它不是有效的HTML

$("input[type='radio']:checked").each(function( index ) {
     $('#output').append($(this).attr('id')+' is checked<br />');
});
$(“输入[type='radio']:选中”)。每个(函数(索引){
$('#output').append($(this).attr('id')+'被选中
); });

下面是一个适用于您的JSFIDLE:

id
属性必须是唯一的!(除非您使用html5,否则属性不应该以数字开头)你能告诉我在最新的jQuery版本中它在哪里仍然被弃用吗?没问题,谢谢分享链接和帮助我的努力,我将删除我的。我删除了true,现在它的计算结果总是false!谢谢!我必须删除“form id”,然后它工作了…所以:if($('input:radio[name=match'+x+')。prop('checked'))这也适用于thnaks.“.is(':checked')”和“prop('checked')”之间的区别是什么-我应该一个接一个地使用吗?在效率方面不是100%确定,但我发现它更具可读性,因此我可以回顾我的代码并更容易地了解发生了什么。奇怪的是,代码可以工作,我可以检测到哪些单选按钮被选中。然后我的脚本会清除以前选中的所有选中按钮。然后,如果我重新选择一些按钮没有一个按钮被检测到被选中!好的,我解决了它,在这样做的过程中,我想我已经回答了“.is(':checked')”和“prop('checked')”之间的区别问题。后来检测到的输入是我通过jquery设置的。前一个检测到的输入是我用鼠标选择的。没有“.is”)(“:checked”)“如果之前我用脚本清除了任何选定按钮,则未检测到输入。
for (var i = 0, len = 4; i < len; i++) {
    if ( $('input[name=match' + i + ']:checked').val() ) {
        alert("Checked");
    }
}
<input type="radio" class="win" id="50" value="1" name="match1" />
<input type="radio" class="cover" id="50" value="2" name="match1" />

<input type="radio" class="win" id="51" value="1" name="match2" />
<input type="radio" class="cover" id="51" value="2" name="match2" />

<input type="radio" class="win" id="52" value="1" name="match3" />
<input type="radio" class="cover" id="52" value="2" name="match3" />

<button id="getChecked"></button>

function alertChecked() {
   for (var i = 0, x = 4; i < x; i++) {
       if ( $('input[name=match' + i + ']').is(':checked')) {
           alert("Checked");
        }
    }
}

$('#getChecked').click(function() {
    alertChecked();
});
$("input[type='radio']:checked").each(function( index ) {
     $('#output').append($(this).attr('id')+' is checked<br />');
});