简体   繁体   English

为什么单选按钮或复选框按钮数组上的element.type返回“ undefined”

[英]Why element.type on an array of radio or checkbox buttons returns “undefined”

I'm facing a very strange situation here. 我在这里面临一个非常奇怪的情况。 Let's say I have a form with three input elements of type radio , just like this 假设我有一个带有三个类型为radio的输入元素的表单,就像这样

<form>
    <input type="text" name="donor" value="" />Heart
    <input type="radio" name="organ" value="10" />Heart
    <input type="radio" name="organ" value="20" checked="checked" />Lungs
    <input type="radio" name="organ" value="30" />Kidney    
</form>

and I thought this line 我以为这条线

alert("input type: " + document.forms[0].organ.type);

would return type radio instead of undefined . 将返回radio类型而不是undefined Though, this one 虽然,这个

alert("input type: " + document.forms[0].organ[0].type);  

returns type radio and of course it's not what I want. 返回radio类型,当然这不是我想要的。 You may see the sorcery here . 您可能会在这里看到魔术。

Does anyone know what's going on? 有人知道发生了什么吗?

1st UPDATE 第一次更新

The same thing will occur if we deal with checkboxes, which means that element type on an array of radios or checkboxes buttons cannot be determined without specifying an index. 如果我们处理复选框,则会发生相同的事情,这意味着如果不指定索引,就无法确定单选或复选框按钮数组上的元素类型。

In other words, this snippet 换句话说,这个片段

var i, form = document.forms[0], fields = ['donor', 'organ'];

for(i = 0; i < fields.length; i++) {
    switch(form[fields[i]].type) {
        case 'radio':
            alert('(a) - I can beat up Chuck Norris');
            // anyway, no one hears this :)
            break;
        case 'checkbox':
            break;
        case 'text':
            alert('(a) - Chuck Norris is unbeatable');
            break;
    }
}

won't behave as one may expect. 不会像人们期望的那样表现。 So, I guess, looping through the form elements is the only solution left. 因此,我想,遍历表单元素是剩下的唯一解决方案。 Well, it's not - check the 2nd update. 好吧,不是-检查第二次更新。

for(i = 0; i < form.elements.length; i++) {
    switch(form.elements[i].type) {
        case 'radio': 
            alert('(b) - Nobody can beat up Chuck Norris');
            // now, check if this element is in fields[] 
            // and do something  
            break;
        case 'checkbox':
            break;
        case 'text':
            alert('(b) - Chuck Norris is unbeatable');
            break;
    }
}

Here is the fiddle . 这是小提琴

2nd UPDATE 第二次更新

In a meantime I found a pretty simple way to access and query form elements looping through fields object (in my case) and not looping through all elements. 同时,我找到了一种非常简单的方法来访问和查询表单元素(在我的情况下)遍历fields对象,而不遍历所有元素。

var form = document.forms[0], fields = ['donor', 'organ'];

for (var key in fields) {
    switch(form[key].type){
        case 'select-one':
            break;
        case 'select-multiple':
            break;
        case 'checkbox': // we are dealing with a single checbox button 
            break;
        case 'text': case 'textarea': case 'hidden': case 'password':
            break;
        default:
            if(form[key][0].type == radio' || form[key][0].type == radio' == 'checkbox') {
                // we're dealing with an array of radio or checkbox buttons, otherwise
            }else{
                console.log("Something went wrong!");
            }
            break;
    }
}

The secret can be seen if you add this line: 如果添加以下行,则可以看到秘密:

alert(document.forms[0].organ.toString());

and you'll see that it creates a RadioNodeList , which is an array-like structure. 并且您将看到它创建了RadioNodeList ,它是一个类似数组的结构。

Because all related radio buttons have the same name, yet are different elements, you have to access them in this way to set the status. 由于所有相关的单选按钮都具有相同的名称,但元素不同,因此必须以这种方式访问​​它们以设置状态。

If you truly MUST access them without using array syntax [0] , you could give each radio button a unique ID: 如果您确实必须在不使用数组语法[0]情况下访问它们,则可以为每个单选按钮赋予唯一的ID:

<form>
    <input type="radio" id="radio_1" name="organ" value="10" />Heart
    <input type="radio" id="radio_2" name="organ" value="20" checked="checked" />Lungs
    <input type="radio" id="radio_2" name="organ" value="30" />Kidney
</form>

<script>
    alert("input type: " + document.forms[0].organ[0].type);

    alert(document.forms[0].organ.toString());

    alert("input type: " + document.getElementById("radio_1").type);
</script>

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

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