简体   繁体   English

Javascript单选按钮在FF和Chrome中有效,但在IE中不起作用

[英]Javascript radiobutton works in FF and Chrome but not in IE

I have to validate if the radiobox is checked. 我必须验证是否选中了单选框。

HTML 的HTML

<input style="width:20px;" id="radio1" type="radio" name="benvoor" class="benvoor"  value="Ik ben voor" /> <label for="radio1">Ik ben voor. </label><br />
<input style="width:20px;" id="radio2" type="radio" name="benvoor" class="benvoor"  value="Ik ben tegen" /> <label for="radio2">Ik ben tegen.</label>

JavaScript/jQuery JavaScript / jQuery

//Assume no radio checked first
var benvoor = false;
for (i in aanmeldform.benvoor) {
    // Is the element checked?
    if (aanmeldform.benvoor[i].checked) {
        //Choice has been made
        benvoor = true;
        // Cancel the loop if the checked element is found
        break;
    }
}

// If no choice has been made, put it in the errorList
if (!benvoor) errorList.push("benvoor");

// Cancel submit if errors are found
if (errorList.length > 0) {
    document.getElementById("errorMessage").innerHTML = "Graag uw keuze maken";    
    $("#radiobutton label").addClass("rood");
    $("html, body").animate({
        scrollTop: $(this).offset().top
    }, 1000);
    return false;
}​

Given that you're using jQuery, you can do this: 假设您使用的是jQuery,则可以执行以下操作:

if ($(':radio[name="benvoor"]:checked').length === 0) {
   // none are checked, do something
}

That is, find all radio buttons of that name that are checked, and if the resulting jQuery object has a length of 0 then none are checked. 也就是说,找到所有已选中该名称的单选按钮,如果结果jQuery对象的长度为0,则不检查任何单选按钮。

Simple demo: http://jsfiddle.net/WKKby/ 简单的演示: http : //jsfiddle.net/WKKby/

You don't show much of your html, but from your JS it looks like the radio buttons are inside an element with the id "radiobutton" so you might want to include that in your jQuery selector: 您没有显示太多的html,但是从JS看来,单选按钮似乎位于id为“ radiobutton”的元素内,因此您可能希望将其包含在jQuery选择器中:

if ($('#radiobutton :radio[name="benvoor"]:checked').length === 0) {

If you are using jquery anyway, probably go with @nnnnnn answer, but there is your code slightly modified in a jsFiddle: http://jsfiddle.net/p9bs3/5/ 无论如何,如果您使用的是jquery,则可能要使用@nnnnnn答案,但是在jsFiddle中您的代码有一些修改: http : //jsfiddle.net/p9bs3/5/

var benvoor = false;
for (var i =0;i < aanmeldform.benvoor.length;i++) {
    // Is the element checked?
    if (aanmeldform.benvoor[i].checked) {
        //Choice has been made
        benvoor = true;
        // Cancel the loop if the checked element is found
        break;
    }
}

It seems IE handle formcollections different than a regular array. 看来IE处理的形式集合不同于常规的数组。 The following code produces two different results in chrome and IE. 以下代码在chrome和IE中产生两个不同的结果。

<form id="frm">
    <input type="radio" name="rdio">
    <input type="radio" name="rdio">
</form>

Script: 脚本:

var arr = [1,2];
for(i in arr){
    console.log(i);    
}

console.log('-----');
for(i in frm.rdio){
    console.log(i);    
}

Chrome

0
1
-----
0
1
length
item

IE IE浏览器

0 
1 
------------ 
rdio 
length 
item 
namedItem 

for in loops are often cause for problems in javascript imo, use helpers like jquery's each or do a regular for loop, like I have in the example above. for循环通常会在javascript imo中引起问题,请使用jquery的辅助程序,或使用常规的for循环,就像上面的示例中一样。

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

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