获取兄弟文本输入值 [英] Getting sibling text input value
问题描述
我有以下HTML
< li class ='user_attributes'>< b>使用者名称:< / b> {{username}}
< input class ='user_input form-control edit_fields {{_id}}'id ='username_field'type =textname ='username'placeholder =username> ;
< button type =submitclass =btn btn-default submit_button edit_fields {{_id}}id ='update_username'> update< / button>
< / li>
我希望得到输入字段的值,只要 < li>
的提交按钮被点击(不是具有相同名称的其他输入字段的值)。
我有以下jQuery,但它全部返回undefined: 编辑 这是一个使用(Meteor.js)的框架Im和'this'的范围的问题 https://jsfiddle.net / zy7qy3v5 / 3 / 不要在jiblings函数中放置一个不必要的jquery对象。您的选择器正在返回每个兄弟。 与 请参阅 https://jsfiddle.net/8x04nbyx/5/ p> I have the following HTML And I'd like to get the value of the input field whenever that I have the following jQuery, but all of it returns undefined: EDIT this is an issue of the framework Im using (Meteor.js) and the scope of 'this' Try that https://jsfiddle.net/zy7qy3v5/3/ Dont put an unecessary jquery object in the siblings function. Your selector is returning every siblings. is not the same as See https://jsfiddle.net/8x04nbyx/5/ 这篇关于获取兄弟文本输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ 'click #update_username':function(ev,template){
ev.preventDefault(); $(b)
$ b // var username_field = template。$('input [id =username_field]')。val();
// var username_field = $(ev.target).find('[name = message]')。val();
var input_field = $(this).siblings($('input [id =username_field]'))。val();
console.log(input_field);
// Meteor.call('updateUsername',this._id,username_field);
code
$ b
$('button')。click(function(){
var value = $(this).siblings('input').val();
alert(value);
})
$('button')。siblings('input')//返回1个元素
$(this).siblings($('input [id =username_field]'))//返回2元素
<li class='user_attributes'><b>username: </b>{{username}}
<input class='user_input form-control edit_fields {{_id}}' id='username_field' type="text" name='username' placeholder="username">
<button type="submit" class="btn btn-default submit_button edit_fields {{_id}}" id='update_username'>update</button>
</li>
<li>
's submit button is clicked (not the value of other input fields with the same name). 'click #update_username': function(ev, template){
ev.preventDefault();
// var username_field = template.$('input[id="username_field"]').val();
// var username_field = $(ev.target).find('[name = message]').val();
var input_field = $(this).siblings($('input[id="username_field"]')).val();
console.log(input_field);
// Meteor.call('updateUsername', this._id, username_field);
}
$('button').click(function(){
var value = $(this).siblings('input').val();
alert(value);
})
$('button').siblings('input') //Returns 1 element
$(this).siblings($('input[id="username_field"]')) //Returns 2 element