我建議:
// 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);
});
謝謝,我測試你的代碼,但它似乎沒有觸發點擊單選按鈕:https://jsfiddle.net/wpLuenLv/3/ – Nima