简体   繁体   English

使用jQuery动态获取名称属性

[英]getting name attribute dynamically using jquery

I have a div block where I am trying to find the name of the input element which was clicked and then update the value using jQuery: 我有一个div块,我试图在其中查找被单击的输入元素的名称,然后使用jQuery更新值:

 $(document).ready(function(){ $(this).click(function(){ console.log($(this).attr('name')); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input type="text" name="name1" id="id1">TestData1</input> <input type="text" name="name2" id="id2">TestData2</input> </div> 

When I try to find the input element which was clicked I get undefined . 当我尝试找到被单击的输入元素时,我undefined

I am assuming it may be because I am checking click event on this and then again trying to get the attr name of it, but since it can increase to at least 5 input elements and these are being fetched dynamically, I cannot bind the id/class with click event. 我认为这可能是因为我正在检查click事件,然后再次尝试获取其attr名称,但是由于它可以增加到至少5个输入元素并且这些元素是动态获取的,因此我无法绑定id /带有点击事件的课程。

The this has no context inside ready function : this在ready函数中没有上下文:

$(this).click(function(){
_^^^^^___________________
   console.log($(this).attr('name'));
});

You should use input : 您应该使用input

$('input').click(function(){
   console.log($(this).attr('name'));
});

NOTE ALSO : the input tag is self closed : 注意:输入标签是自封闭的:

<input type="text" name="name1" id="id1" value='TestData1'/>
<input type="text" name="name2" id="id2" value='TestData2'/>

I hope this will helps. 我希望这会有所帮助。

 $(document).ready(function(){ $('input').click(function(){ console.log($(this).attr('name')); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input type="text" name="name1" id="id1" value='TestData1'/> <input type="text" name="name2" id="id2" value='TestData2'/> </div> 

Why not bind by element instead? 为什么不按元素绑定呢?

$("input").click(function(){
   console.log($(this).attr('name'));
});

You are binding the click to document (Simply entire web page) and document doesn't have attribute called name . 您将点击绑定到document (仅整个网页),而文档没有名为name属性。 It will return undefined on console. 它将在控制台上返回undefined

As other answers suggests, I'll also suggest binding click event to input instead of document . 正如其他答案所暗示的那样,我还将建议将click事件绑定到input而不是document

If you want to bind click with document and find the target clicked element by using event which will be automatically available as an argument on click handler. 如果您想将click与document绑定,并通过使用event查找目标clicked元素,该event将自动用作click handler的参数。 Try with following snippet 尝试以下代码段

 $(document).ready(function(){ $(this).click(function(event){ console.log($(event.target).attr('name')); }); }); 
 <div> <input type="text" name="name1" id="id1">TestData1</input> <input type="text" name="name2" id="id2">TestData2</input> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Here you access event target element on which you clicked and using it to get name attribute value. 在这里,您访问单击的事件目标元素,并使用它来获取name属性值。 It returns name attribute value if your target element has name attribute else undefined will be returned. 如果目标元素具有name属性,则返回name属性值,否则将返回undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM