jQuery如何使用jQuery检索输入文本字段的名称 $(文档).ready(函数(){ var pValue=$(“.blah”).find(“p”).html(); 警告(“段落标记中的信息为###”+pValue);`在此处输入代码` $(“.blah”).find($(“输入:文本”).each(函数(){ 警报(“输入文本字段中的值为#####“+此.Value”); }); });
呵呵 问题: 当我运行上面的HTML代码时,会收到以下警报jQuery如何使用jQuery检索输入文本字段的名称 $(文档).ready(函数(){ var pValue=$(“.blah”).find(“p”).html(); 警告(“段落标记中的信息为###”+pValue);`在此处输入代码` $(“.blah”).find($(“输入:文本”).each(函数(){ 警报(“输入文本字段中的值为#####“+此.Value”); }); });,jquery,Jquery,呵呵 问题: 当我运行上面的HTML代码时,会收到以下警报 段落标记中的信息是####he 输入文本字段中的值为#####abc 输入文本字段中的值为#####xyz 我希望检索输入文本字段名称(用户名和id),并将其动态地放入警报中,以便我的警报看起来如下图所示。我想要这个功能,因为如果用户输入用户名而不是id,我想显示他的id是空的 段落标记中的信息是####he 用户名中的值为#####abc id内的值为#####xyz 事先非常感谢。如果您有这样一个字段: <!DOCT
- 段落标记中的信息是####he
- 输入文本字段中的值为#####abc
- 输入文本字段中的值为#####xyz
- 段落标记中的信息是####he
- 用户名中的值为#####abc
- id内的值为#####xyz
事先非常感谢。如果您有这样一个字段:
<!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>
<input id="tb" type="text" name="userName" value="abc"/>
但是,您的输入格式不正确,并且没有Id或类,在其中循环将非常低效。我不确定您的问题是什么。但是,要获取所选控件的名称,请执行以下操作:
$('#tb').attr("name");
您的标记有一系列错误,首先修复这些错误,然后获取任何输入字段的名称,只需说
var nameValue = $('.blah').attr('name');
试试这个
$("inputIdOrValidSelector").attr('name');
呵呵
$(文档).ready(函数(){
var pValue=$(“.blah”).find(“p”).html();
警报(“段落标记中的信息为####”+pValue);
$(“.blah”).find($(“输入:文本”).each(函数(){
警报(“在“+this.name+”中的值是“+this.Value”);
});
});
试试:
工作演示-您的
中的这个对象与DOM中的输入元素相对应。此元素具有可用的name属性。例如:
alert("Value inside the userName is " + $("input:text[name=userName]").val());
alert("Value inside the id is " + $("input:text[name=id]").val());
我也创建了这段代码的一部分
$(".blah").find($("input:text")).each(function(){
alert("Value inside "+$(this).attr("name")+" is #### " + this.value);
});
alert("Value inside the userName is " + $("input:text[name=userName]").val());
alert("Value inside the id is " + $("input:text[name=id]").val());
alert("Value inside the " + this.name + " is #### " + this.value);