简体   繁体   English

jQuery:获取最接近按钮的值

[英]Jquery: get value of closest button

Here its my html code 这是我的html代码

<div class="panel-footer">
<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="79">Send</button>
</span>
</div>

<div class="input-group">
<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend">
<span class="input-group-btn">
<button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="80">Send</button>
</span>
</div>
</div>

And here its my jquery code 这是我的jQuery代码

$(document).on('keypress', '.message', function (e) {
    if (e.which == 13) {
    var msg = $(this).val();
    var id = $(this).closest('span').next('button.btn_chat').val();
    alert(msg);
    alert(id);
}
e.preventDefault();
}); 

stuck on id it shows always undefine. 卡在id上,它始终显示未定义。

how can I get button value 我如何获得按钮值

I have inserted code. 我已插入代码。 here it is with id not giving undefined 在这里它的ID不给未定义

 $(document).on('keypress', '.message', function (e) { if (e.which == 13) { var msg = $(this).val(); var id = $(this).next('span').find('button.btn_chat').val(); alert(msg); alert(id); } e.preventDefault(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="panel-footer"> <div class="input-group"> <input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend"> <span class="input-group-btn"> <button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="79">Send</button> </span> </div> <div class="input-group"> <input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend"> <span class="input-group-btn"> <button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="80">Send</button> </span> </div> </div> 

the span is the next sibling and the button is a descendant of the span so using closest and next will not work. 范围是下一个同级,并且按钮是范围的后代,因此使用最近下一个将不起作用。

The closest() is used to find a ancestor element and next() to find the next sibling element. 最近的()用于查找祖先元素,而下一个()用于查找下一个兄弟元素。

In your case you should use .next() to find the span and use .find() to find the descendant button 在您的情况下,应使用.next()查找跨度,并使用.find()查找后代按钮

var id = $(this).next('span').find('button.btn_chat').val();

Demo: Fiddle 演示: 小提琴


Another easier solution is to use a data-* attribute to store the value in the input element itself like 另一个更简单的解决方案是使用data-*属性将值存储在输入元素本身中,例如

<input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend" data-id="79">

then 然后

var id = $(this).data('id');

$(this).closest('span') will search for the nearest parent span tag of the text box, instead you need adjacent span tag. $(this).closest('span')将搜索文本框最近的父span标签,而您需要相邻的span标签。 Try using next() with find() : 尝试将next()find()

 $(function() { $('input:text.message').on('keypress', function(e) { if (13 !== e.which) return; var btn = $(this).next().find('.btn_chat'); console.log(this.value, btn.val()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="panel-footer"> <div class="input-group"> <input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend"> <span class="input-group-btn"> <button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="79">Send</button> </span> </div> <div class="input-group"> <input id="btn-input" class="form-control input-sm chat_input message" type="text" placeholder="Write your message here..." ng-model="messageToSend"> <span class="input-group-btn"> <button id="btn_chat" class="btn btn-primary btn-sm btn_chat" value="80">Send</button> </span> </div> </div> 

尝试使用$(this).parent()。closest('span')。next('button.btn_chat')。val();

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

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