2013-04-16 40 views
4

在我當前的項目中,我有兩個文本字段,其中用戶分別輸入需求和區域。抓住這些值,我必須做一個Ajax調用,並從數據庫中獲取必要的數據並返回。當任何文本字段中的輸入超過4個字符時,必須調用ajax調用。只要只有一個文本字段,我就沒有任何問題。將輸入字段的兩個值傳遞給腳本併合並搜索

<input type="text" value="requirement" onkeyup="function1(this.value)" /> 
<input type="text" value="area" onkeyup="function2(this.value)" /> 

<script> 
function function1(valuetosearch) 
{ 
//make the ajax call 
} 
</script> 

<script> 
function function2(valuetosearch2) 
{ 
//make the ajax call 
} 
</script> 

如何組合兩個腳本並將數據作爲數組傳遞給ajax?
P.S腳本編寫的主要原因是對兩個輸入字段進行搜索。例如,如果有人進入房屋,車輛在需求區域和地點1,地區2。結果搜索應該顯示以下
1)PLACE1房子
2)PLACE1使用車輛
3)place2房子
4)place2車輛

回答

1

試試這個

<input type="text" value="requirement" onkeyup="functionABC()" id="First" /> 
<input type="text" value="area" onkeyup="functionABC()" id="Second" /> 

<script> 
function functionABC() 
{ 
    var searchString=$("#First").val()+" "+$("#Second").val(); 
    //make the ajax call 
} 
</script> 

將「searchString」作爲參數傳遞。

+0

好吧..試試吧:) –

+0

謝謝..它的工作。 :) –

3

可以爲每個elemnt設置ID,並得到了對方的價值

結果
document.getElementById("id1").value 

,然後使Ajax請求

+0

感謝。是個好主意。 –

2

http://jsfiddle.net/JMpgU/

$("input").keyup(function() { 
    var str = $(this).val() + " " + $(this).siblings().val(); 
    alert(str); 
    //make the ajax call 
}); 

與任何數量的輸入: http://jsfiddle.net/JMpgU/2/

$("input").keyup(function() { 
    var strings = $(this). 
        siblings(). 
        addBack(). 
        map(function() { 
         return $(this). 
          val(); 
        }). 
        toArray(); 
//make the ajax call 
}); 
+0

謝謝我發現我的問題類似於你的解決方案.. :) –

相關問題