attr() 在无线电返回:未定义不是一个函数

attr() on radio return: undefined is not a function

本文关键字:函数 一个 无线电 返回 attr 未定义      更新时间:2023-09-26

我有这个代码

$.each($('input:checked', '#components-holder'), function(index, input){
    console.log(input.attr('value'));
});

我得到了这个错误:

undefined is not a function

如何迭代页面中的所有广播并获得价值?

作为input发送到回调的对象不是 jQuery 对象,因此不能使用 jQuery 方法。您需要将其转换为 jQuery 对象才能使用 jQuery 方法:

console.log($(input).attr('value'));

或者使用本机 DOM 属性:

console.log(input.value);

或者,您可能希望使用 map 来获取适当的值:

var values = $('#components-holder input:checked').map(function(index, input) {
    return input.value;
}).get();

values现在是一个包含所有相关值的数组。