我正在尝试选择某个元素内的所有输入字段。我将此元素传递给一个函数,但我不知道如何从那里开始:
$('._save, .btn-success').click(function(){
saveForm($('.dashboard_container'));
});
//This functions has to select all the input fields inside the given element
function saveForm(that) {
var input = $(that, 'input'); //This does not seem to work
input.each(function(){
//Do something
})
}
如何将变量和选择器链接在一起?
最佳答案
上下文选择器采用与您使用的参数相反的方式:
function saveForm($that) {
var $input = $('input', $that);
$input.each(function(){
//Do something
})
}
您还可以在传入 jQuery 对象时使用 find()
方法:
var $input = $that.find('input');
关于javascript - 通过在函数中传递变量来选择元素 - jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667179/