Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于获取字段属性的JQuery通用工具。_Javascript_Jquery - Fatal编程技术网

Javascript 用于获取字段属性的JQuery通用工具。

Javascript 用于获取字段属性的JQuery通用工具。,javascript,jquery,Javascript,Jquery,我试图编写一个通用javascript工具,用JQuery获取表单字段名、id及其值 <div id="StudentForm"> <div><input id='name' name='name' value='John' /></div> </div> 例如,我想获取'StudentForm'div中的所有字段,输出其id、名称和值。如何编写通用工具包或API 我喜欢JQuery,所以我更喜欢JQuery解决方案 HTML:

我试图编写一个通用javascript工具,用JQuery获取表单字段名、id及其值

<div id="StudentForm">
<div><input id='name' name='name' value='John' /></div>
</div>

例如,我想获取'StudentForm'div中的所有字段,输出其id、名称和值。如何编写通用工具包或API

我喜欢JQuery,所以我更喜欢JQuery解决方案

HTML:


我希望这就是你要找的

您可以编写一个简单的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提供的功能和方法

(function ($) {
    $.fn.getAttributes = function () {
        return [$(this).attr('id'), $(this).attr('name'), $(this).val()];
    }
}(jQuery));
$('#StudentForm').find('input').each(function () {
    alert($(this).getAttributes());
});