我正在尝试使用 JQuery 获取控件类型,下面是我正在使用的代码。
$('#selCity').attr('type')
其中 selCity 是 select 类型。当我尝试上面的代码时,它返回未定义,但是当我使用带有 Javascript 的替代代码时,它返回正确的类型。
请查看这个 fiddle 以清楚地理解它:http://jsfiddle.net/Ye8e9/
有人可以建议我如何使用 JQuery 正确地实现这一目标吗?这是 JQuery 的问题还是我弄错了?
最佳答案
使用
$('#selCity').prop('type')
As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.
关于javascript - 使用 Javascript 和 JQuery 获取控件的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13493251/