gpt4 book ai didi

jQuery 1.6 类型属性未定义

转载 作者:行者123 更新时间:2023-12-01 08:22:49 25 4
gpt4 key购买 nike

我似乎无法获取 jQuery 1.6 中的 type 属性

<div id="div_id">
<input type="text" value="foo" />
<input type="text" value="foo" />
<input type="text" value="foo" />
<input type="text" value="foo" />
<input type="checkbox" checked="checked" />
</div>

和jquery

$.each('#div_id input',function(index,value){
var input_type = $(this).prop('type')
alert(input_type);
/*
switch(input_type) {
case 'checkbox':
$(this).prop('checked',false);
break;
//more cases here
default:
this.value = '';
}*/
});

查看我的fiddle

最佳答案

这是因为你误解了$.each()函数,它接受数组或对象(而不是选择器)。当您将字符串传递给 $.each() 时,jQuery 会迭代字符串中的所有字符(在大多数浏览器中)。

要解决此问题,您可以将选择器传递给 jQuery,然后在 $.each() 中使用结果,或者对结果调用 .each():

$('#div_id input').each(function(index,value){
var input_type = $(this).prop('type');
/* ... */
});

查看您的updated fiddle .

如果您有胆量,可以在函数内放弃 jQuery 并直接访问属性,从而提高效率并减少代码:

var input_type = this.type;

关于jQuery 1.6 类型属性未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6230604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com