<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
var pValue = $(".blah").find("p").html();
alert(" Information in Paragraph tag is ### " + pValue);`enter code here`
$(".blah").find($("input:text")).each(function(){
alert("Value inside the Input text field is #### " + this.value);
});
});
</script>
</head>
<body>
<div class="blah">
<p>he he he</p>
<input "type = text name ="userName" value = "abc"/>
<input "type = text name ="id" value = "xyz"/>
</div>
</body>
</html>
问题:
当我运行上面的 HTML 代码时,我收到以下警报
- 段落标签中的信息是### he he he
- 输入文本字段内的值为 #### abc
- 输入文本字段内的值为 #### xyz
我想检索输入文本字段名称(用户名和 ID)并将其动态放置在警报中,以便我的警报如下所示。我想要这个功能,因为如果用户输入他的用户名而不是 ID。我想显示他的id为空。
- 段落标签中的信息是### he he he
- userName 内的值为 #### abc
- id 内的值为 #### xyz
提前非常感谢。
最佳答案
如果您有这样的字段:
<input id="tb" type="text" name="userName" value="abc"/>
你想在 JQuery 中获取 name 属性,它看起来像这样:
$('#tb').attr("name");
但是,您的输入格式错误,并且它们没有 ID 或类,并且循环遍历它们的效率极低。
关于jQuery 如何使用 JQuery 检索输入文本字段的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7045530/