实际上,我有超过 8 个输入,HTML 中有 8 个不同的 id,我想将 jquery 对象传递给 onlblur 事件函数,所以我不需要创建 8 个重复函数,只创建 1 个脚本函数。 我已经在 Stack Overflow 上努力搜索了几个小时,但找不到我的问题的答案,或者也许我只是 jquery 的新手。希望您能帮助我并提前致谢...
function fill(t,xx,zz) {
$(xx).val(t);
setTimeout("$(zz).hide();", 200);
}
<input type="text" id="inputString" size="50" value="" onkeyup="lookup(this.value);" onblur="fill(this.value,'#inputString','#suggestions');" />
<div class="suggestionsBox" id="suggestions" style="display: none;">
为了让您更好地理解代码,这是真正有效的原始代码,并且仅适用于 1-input html 标签。我计划在 8 输入 html 标签上仅使用一个函数。
<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
function lookup(xString) {
if(xString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("db_rpc.php", {queryString: ""+xString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(t) {
$('#inputString').val(t);
setTimeout("$('#suggestions').hide();", 200);
}
最佳答案
使所有元素共享相同的类。
<div id="idOne" class="toBlur" />
<div id="idTwo" class="toBlur" />
<div id="idThree" class="toBlur" />
告诉 jQuery,当触发 blur
事件时,您希望将相同的函数应用于该类的每个项目。
$(".toBlur").blur(function() {
// Do whatever.
});
此外,如果您要使用 jQuery(它在您的问题标签中),您不应该像以前那样在 HTML 中分配回调。
关于javascript - 如何将多个jquery对象传递给js函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13949381/