在javascript中选择同名单选按钮

在javascript中选择同名单选按钮,javascript,Javascript,我需要在JavaScript中的一个变量中添加2个无线电输入。下面的代码只识别第一个输入,即document.forms[“myForm”][“Suited”][0]onclick应通过单选按钮触发。我可以将代码复制成2个变量和2个onclick事件,但这并不理想。任何想法都将不胜感激 请注意,我不能在我的案例中使用getElemntbyId或getElementbyTagName,因为在我的项目中对html的访问受到限制,所以我只能通过nametag触发 var inputs = docume

我需要在JavaScript中的一个变量中添加2个无线电输入。下面的代码只识别第一个输入,即
document.forms[“myForm”][“Suited”][0]
onclick
应通过单选按钮触发。我可以将代码复制成2个变量和2个onclick事件,但这并不理想。任何想法都将不胜感激

请注意,我不能在我的案例中使用getElemntbyId或getElementbyTagName,因为在我的项目中对html的访问受到限制,所以我只能通过
name
tag触发

var inputs = document.forms["myForm"]["satisfied"][0] || document.forms["myForm"]["satisfied"][1];

inputs.onclick = function() {
    document.forms["myForm"]["check"].disabled= false;
}
用于选择具有属性的图元

var radios=document.querySelectorAll('input[name=“successed]”);
//迭代它们并绑定事件
对于(变量i=0,len=radios.length;i
我建议:

// retrieving all elements with the name of 'satisfied':
var inputs = document.getElementsByName('satisfied');

// defining a function so that multiple elements can
// be assigned the same function:
function enable () {

    // iterating over the inputs collection:
    for (var i = 0, len = inputs.length; i<len; i++) {
        // updating the 'disabled' property to false,
        // thus enabling the inputs:
        inputs[i].disabled = false;
    }
}

// iterating over the inputs collection:
for (var i = 0, len = inputs.length; i<len; i++) {
    // binding the enable() function as the
    // event-handler for the click event:
    inputs[i].addEventListener('click', enable);
}

谢谢你的描述性回答。我尝试了你的第二个选项,但没有得到结果,你可以在这里看到:每次单击单选按钮时,复选框都应该启用。谢谢,我测试了你的代码,但它似乎不会在单击时触发单选按钮:
// retrieving all elements with the name of 'satisfied':
var inputs = document.getElementsByName('satisfied');

// defining a function so that multiple elements can
// be assigned the same function:
function enable () {

    // iterating over the inputs collection:
    for (var i = 0, len = inputs.length; i<len; i++) {
        // updating the 'disabled' property to false,
        // thus enabling the inputs:
        inputs[i].disabled = false;
    }
}

// iterating over the inputs collection:
for (var i = 0, len = inputs.length; i<len; i++) {
    // binding the enable() function as the
    // event-handler for the click event:
    inputs[i].addEventListener('click', enable);
}
function enable() {
    // using Array.from() to convert the collection returned by
    // document.getElementsByName() into an array; over which we
    // iterate using Array.prototype.forEach().

    // 'this' is supplied from EventTarget.addEventListener();
    // and allows us to retrieve the name, and the associated
    // 'group' of elements for the specific input; meaning this
    // same function can be bound to multiple 'groups' of elements
    // without interfering with the other 'groups':
    Array.from( document.getElementsByName( this.name ).forEach(function (el) {
        // el: the current element in the Array over which
        // we're iterating.

        // updating the 'disabled' property to false:
        el.disabled = false;
    });
}

// as above, except we supply the 'name' explicitly:
Array.from( document.getElementsByName('satisfied') ).forEach(function (el) {
    // binding the enable() function as the event-handler
    // for the click event:
    el.addEventListener('click', enable);
});