[英]jQuery, multiple inputs and onBlur
所以我有2个输入。 两者都是为了“自动完成”。 我也有2个功能几乎可以完成相同的工作...每个输入都调用不同的onBlur函数。 问题在于,第二个选择第二个输入的“建议”后,它会填充第一个。 如何使第二个填充第二个?
可能有更好的方法,但是我的jQuery / Javascript知识有限,我的时间不多了...谢谢!
编码:
function fillLocation(thisValue) {
$('#location-box').val(thisValue);
setTimeout("$('#suggestionsLocation').fadeOut('fast');", 200);
}
function fillTerms(thisValue) {
$('#terms-box').val(thisValue);
setTimeout("$('#suggestionsTerms').fadeOut('fast');", 200);
}
<input type="text" name="search_location" id="location-box" value="" onkeyup="suggestLocation(this.value);" onblur="fillLocation();" autocomplete="off" />
<input type="text" name="search_terms" id="terms-box" value="" onkeyup="suggestTerms(this.value);" onblur="fillTerms();" autocomplete="off" />
我想我假设您想为具有相似功能的两个输入触发击键和模糊事件,而这又会影响相同的输入。
这将是一种方式。 将事件绑定到javascript中,而不是HTML中,并运行正确的代码
$('document').ready(function() {
$('#location-box,#terms-box')
.bind('keyup', function() {
// do your keyup thing using $(this)
// to refer back to the input
//$('body').append($(this).attr('id') + ': keyup<br />');
var theValue = $(this).val();
})
.bind('blur', function() {
// do your blur thing using $(this)
// to refer back to the input
//$('body').append($(this).attr('id') + ': blur<br />');
})
});
<input type="text" name="search_location" id="location-box" value="" autocomplete="off" />
<input type="text" name="search_terms" id="terms-box" value="" autocomplete="off" />
编辑:
取消注释$('body')行以查看正在发生的事件。
希望这就是您想要的。
编辑:添加到代码到keyup事件中以检索值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.