如何在jquery中通过自定义属性获取输入字段的值
How to get the value of input field by custom attribute in jquery
我完全是jquery的初学者。
我试过这个https://jsfiddle.net/0xLqhufd/2/
<body>
<div>
<input type="text" person="firstName" value="John">
<input type="text" person="birthDate" value="September 1st, 2000">
</div>
</body>
但我在警报中一片空白:
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).text());
}
);
正如jQuery文档中所说。。。
.text()方法不能用于表单输入或脚本
请改用.val()。
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
}
);
工作演示
在void元素中,使用text()
是不合适的。请改用.val()
。input[type=text]
是一个void元素,这意味着它内部不能有任何子节点,包括text nodes
。
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
});
演示
input具有.val()来读取值
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());//will read input value
}
);
我对你的怀疑有一个答案。
您必须更改已使用的脚本,如下所示。
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
}
);
如果你发现我的代码对你有用,请更新我。
Hey you can use like this:-
input "type=text" tags have the values not text.
that's why you have to use .val() instead of .text().
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
});
相关文章:
- 从MySQL数据库中获取输入数据需要两次页面刷新
- 输入字段将't获取更新的值
- 如何从客户端的数组中获取用户输入和返回值
- 在数据库中循环值时,为输入框获取唯一值
- Javascript获取具有不同id的文件数's来自没有多个属性的文件输入元素
- 如何使用html/html5中的用户输入获取完整的地理位置详细信息
- 使用 D3 中用户的输入获取值
- 我如何使用javascript并根据提供的输入获取JSON数据
- 提前输入获取远程数据问题
- 根据递归ul中的输入获取一个表单的序列化数组
- 正在尝试根据用户输入获取总额
- Emberjs:从输入获取数据
- 从任意数量的输入获取JavaScript对象
- 在鼠标输入获取下拉元素的焦点
- 如何使用javascript或jquery为新表单输入获取更新的json文件
- 通过用户输入获取变量
- 我如何从html输入获取属性值
- 另一个选项更直接地从td中的输入获取title属性
- 从命令行输入获取完整的iframe url,并从python变量解析为HTML
- AngularJS FORMs:根据前面的输入获取第二个SELECT BOX的Items