用jquery怎样获得input 的name为name的值

 我来答
内暖兄弟创始人
2017-01-14 · 超过44用户采纳过TA的回答
知道答主
回答量:55
采纳率:0%
帮助的人:69.3万
展开全部
name是input标签的属性值,jQuery提供了attr() 方法用于设置/改变属性值

1
2

$("input:text").attr("name");
$("input:text").prop("name"); // 也可以使用prop()方法获取属性

示例代码如下
创建Html元素

1
2
3
4
5
6
7

<div class="box">
<span>点击按钮获取文本框的name属性值:</span>
<div class="content">
<input type="text" name="test" value="这个文本框的name属性值为test">
</div>
<input type="button" class="btn" value="获取文本框name值">
</div>

设置css样式

1
2
3
4
5

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代码

1
2
3
4
5

$(function(){
$("input:button").click(function() {
alert($("input:text").attr("name"));
});
})
zhang779896660
2017-01-14 · TA获得超过701个赞
知道小有建树答主
回答量:717
采纳率:100%
帮助的人:329万
展开全部
你说的是这样么?<input name="name">
如果是的话: jquery("input[name='name']").val();
但是一般不建议这么用, 这么用大多是用来控制样式,而不是取值
取值这个都需要精确控制,所以设置个ID, 指定获取更好
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式