javascript - jquery中如何通过自定义属性获取输入字段的值

标签 javascript 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 文档中所述...

The .text() method cannot be used on form inputs or scripts

使用 .val() 代替。

$('input[person]').each(function(index) {
    alert(index + ': ' + $(this).val());
  }
);

<强> Working Demo

关于javascript - jquery中如何通过自定义属性获取输入字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36401832/

相关文章:

javascript - 从 react 中获取多个复选框值不起作用

javascript - 3e+3 = 3000 - JavaScript 查询

javascript - 尽管字符串很长,如何保持旋转的横幅文本垂直居中?

javascript - 函数的缓存结果?

javascript - $http请求中的Angular JS默认预定义数据

java - 允许用户使用 Javascript API 下载由 Java applet 生成的文件

jquery - Bootstrap 中带页脚的全高侧边栏

javascript - jQuery 手机 : What is the order of page events triggering?

javascript - 顶部导航栏折叠不起作用

javascript - 仅从段落文本中隐藏字母