用于获取字段属性的JQuery通用工具
JQuery generic tool to get field attributes.
我正在尝试编写一个通用的javascript工具,用JQuery获取表单字段名称、id及其值。
<div id="StudentForm">
<div><input id='name' name='name' value='John' /></div>
</div>
例如,我想获取"StudentForm"div中的所有字段,输出其id、名称和值。如何编写通用工具包或API?
我喜欢JQuery,所以我更喜欢JQuery解决方案。
jsfiddle示例
HTML:
<div id="StudentForm">
<div><input id='name' name='name' value='John' /></div>
<div><input id='name1' name='name1' value='John1' /></div>
<div><input id='name2' name='name2' value='John2' /></div>
<div><input id='name3' name='name3' value='John3' /></div>
</div>
jQuery:
var all = ''; //if you need all at once
$('#StudentForm > div > input').each(
function(){
if(all.length == 0){
all = $(this).val();
}else{
all = all + ':' + $(this).val() ;
}
alert( 'value : '+$(this).val()+''n'+
'name : '+$(this).attr('id')+''n'+
"use $(this).attr('any atribute') for any other attr you need"
);
})
alert(all)
我希望这就是你想要的。。
您可以编写一个简单的jQuery插件
(function ($) {
$.fn.getAttributes = function () {
return [$(this).attr('id'), $(this).attr('name'), $(this).val()];
}
}(jQuery));
像这个一样使用
$('#StudentForm').find('input').each(function () {
alert($(this).getAttributes());
});
在这里摆弄
有关jQuery插件的更多信息,请查看此答案
相关文章:
- jQuery工具验证器自定义效果-添加&消除影响
- "工具提示"jQuery插件坏了
- 用于发现哪个JS/jQuery脚本正在冻结页面的工具或技术
- Jquery工具在Webkit中不起作用
- 如何在jQuery点击函数中添加并显示引导工具提示
- 我可以't使用jQuery更改工具提示引导程序的位置
- jQuery UI工具提示:单击工具提示本身即可关闭
- 带有关闭按钮的javascript/jquery响应工具提示
- 如何在函数内部为jquery工具提示调用不同的var字符串
- Jquery TOOLS库-工具提示
- 如何使用jquery在工具提示上显示pdf文件
- Safari 5.1.7 flexslider/jquery工具冲突z索引错误
- jQuery UI 工具提示位置的奇怪行为
- 使用 jQuery 工具提示插件获取 clientX
- 相当于 Jquery/Backbone.js 中的 Dgrid/Store (Dojo 工具包)
- Jquery 工具提示未随 AJAX 数据加载一起显示
- 悬停工具提示显示详细信息 jquery
- 用于获取字段属性的JQuery通用工具
- jquery与ajax控制工具包冲突2013年9月/10月发布
- jquery UI工具提示的自定义背景图像