jquery 怎么获取name

测试的别回答

name是input标签的属性值,jQuery提供了attr() 方法用于设置/改变属性值
$("input:text").attr("name");
$("input:text").prop("name"); // 也可以使用prop()方法获取属性
示例代码如下
创建Html元素
<div class="box">
<span>点击按钮获取文本框的name属性值:</span><br>
<div class="content">
<input type="text" name="test" value="这个文本框的name属性值为test">
</div>
<input type="button" class="btn" value="获取文本框name值">
</div>
设置css样式
div.box{width:300px;height:250px;padding:10px 20px;margin:20px;border:4px dashed #ccc;}
div.box>span{color:#999;font-style:italic;}
div.content{width:250px;height:100px;margin:10px 0;padding:5px 20px;border:2px solid #ff6666;}
input[type='text']{width:200px;height:30px;border:none;}
input[type='button']{width:120px;height:30px;margin:10px;border:2px solid #ebbcbe;}
编写jquery代码
$(function(){
$("input:button").click(function() {
alert($("input:text").attr("name"));
});
})
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-09-28
$("input").attr("name");//这样获取name的值其他元素的属性的都是这样获取