Javascript 如何将多个jquery对象传递给js函数?

Javascript 如何将多个jquery对象传递给js函数?,javascript,jquery,html,Javascript,Jquery,Html,实际上,我在HTML中有8个以上的输入和8个不同的id&我想将jquery对象传递给onblur事件函数,所以我不需要创建8个重复函数&只创建一个脚本函数。 我花了好几个小时努力搜索堆栈溢出,但找不到问题的答案,或者我对jquery还不熟悉。希望你能帮助我&提前谢谢 function fill(t,xx,zz) { $(xx).val(t); setTimeout("$(zz).hide();", 200); } <input type="text" id="i

实际上,我在HTML中有8个以上的输入和8个不同的id&我想将jquery对象传递给onblur事件函数,所以我不需要创建8个重复函数&只创建一个脚本函数。 我花了好几个小时努力搜索堆栈溢出,但找不到问题的答案,或者我对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;">     
函数填充(t,xx,zz){
$(xx).val(t);
setTimeout(“$(zz).hide();”,200);
}
为了让您更好地理解代码,这是真正有效的原始代码&仅适用于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);
}

函数查找(xString){
if(xString.length==0){
//隐藏建议框。
$(“#建议”).hide();
}否则{
$.post(“db_rpc.php”,{queryString:“+xString+”},函数(数据){
如果(data.length>0){
$(“#建议”).show();
$('#autoSuggestionsList').html(数据);
}
});
}
}//查找
函数填充(t){
$('#inputString').val(t);
setTimeout(“$('#建议”).hide();”,200);
}

使所有元素共享相同的

<div id="idOne" class="toBlur" />
<div id="idTwo" class="toBlur" />
<div id="idThree" class="toBlur" />

另外,如果您要使用jQuery(它在您的问题标签中),您不应该像以前那样在HTML中分配回调。

您可以使用事件处理程序和类:

<input  type="text" class="inputString" />  
<div class="suggestionsBox" id="suggestions" style="display: none;">     

通过将参数传递给两个函数,我使用了8输入标记的1组函数。我放置了一个switch语句来确定输入标记id,如函数fill上所示。我还在函数查找中添加了另一个POST变量。下面是我修改过的代码:

 function lookup(inputString,nn) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else 
    {
        $.post("db_rpc.php", {queryString: ""+inputString+"",nns: ""+nn+""}, function(data) 
        { //nns added a new post variable nns for php mysqli
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
}; // lookup

function fill(xx,n) {
switch(n) {
  case 1:
   $('#namedetails').val(xx); // input id
   break;
  case 2:
   $('#catdetails').val(xx);
   break;
}   
setTimeout("$('#suggestions').hide();", 200);
};     


<td class="absy"><input name="namedetails" type="text" id="namedetails" size="50" value="" onkeyup="lookup(this.value,1);" onblur="fill(this.value,1);"/>
        <div class="suggestionsBox" id="suggestions" style="display: none;">
            <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList">
                &nbsp;
            </div>
        </div>
函数查找(inputString,nn){
如果(inputString.length==0){
//隐藏建议框。
$(“#建议”).hide();
}否则
{
$.post(“db_rpc.php”,{queryString:+inputString+”,nns:+nn+”},函数(数据)
{//nns为php mysqli添加了一个新的post变量nns
如果(data.length>0){
$(“#建议”).show();
$('#autoSuggestionsList').html(数据);
}
});
}
}; // 查找
函数填充(xx,n){
开关(n){
案例1:
$('#namedetails').val(xx);//输入id
打破
案例2:
$(#catdetails').val(xx);
打破
}   
setTimeout(“$('#建议”).hide();”,200);
};     

我只想感谢所有努力回答我问题的人。。。但我还是从你的建议中学到了一些东西。也许我的问题对你们来说有点模糊,可能是因为我对jquery还是新手&只是在长时间休息后才回来编写代码。再次感谢…

我想您正在查找arguments数组$(xx)val(t);-这部分什么也做不了。你想做什么?类似html5占位符选项的内容?函数中的该部分使用jquery-1.2.1.pack.js选择下拉列表建议框,并在选择列表后将其隐藏。这个函数只有在我只使用1个输入html标记的情况下才行。为什么不使用jQuery(self)。next('.suggestionsBox')。delay(200)。fadeOut(0);相反,setTimeout?如果我使用它,它还会更改其他8-inputs$('.stringclass').val(t)的值;在下拉列表建议框中。无论如何,谢谢=)
$('.inputString').on({
    keyup: function() {
        lookup(this.value);
    },
    blur: function() {
        var self = this;
        setTimeout(function() {
            $(self).next('.suggestionsBox').hide()
        },200);
    } 
});
 function lookup(inputString,nn) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else 
    {
        $.post("db_rpc.php", {queryString: ""+inputString+"",nns: ""+nn+""}, function(data) 
        { //nns added a new post variable nns for php mysqli
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
}; // lookup

function fill(xx,n) {
switch(n) {
  case 1:
   $('#namedetails').val(xx); // input id
   break;
  case 2:
   $('#catdetails').val(xx);
   break;
}   
setTimeout("$('#suggestions').hide();", 200);
};     


<td class="absy"><input name="namedetails" type="text" id="namedetails" size="50" value="" onkeyup="lookup(this.value,1);" onblur="fill(this.value,1);"/>
        <div class="suggestionsBox" id="suggestions" style="display: none;">
            <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList">
                &nbsp;
            </div>
        </div>