最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass two values of input fields to script and combine for searching - Stack Overflow

programmeradmin2浏览0评论

In my current project, i have two text fields in which user will input requirement and area respectively. Grabbing those values, i have to do an ajax call and fetch the necessary data from the database and return it. The ajax call has to be made as and when the input exceeds 4 characters in any of the text fields. As long as there was only one text field i didn't have any problem.

<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>

How can i bine the two scripts and pass the data in ajax as an array?
P.S The main reason for scripting is to do a search bining the two input fields. For example if someone enters house, vehicle in requirement field and place1,place2 in area field. The result search should display the result for the following
1) place1 house
2) place1 vehicle
3) place2 house
4) place2 vehicle

In my current project, i have two text fields in which user will input requirement and area respectively. Grabbing those values, i have to do an ajax call and fetch the necessary data from the database and return it. The ajax call has to be made as and when the input exceeds 4 characters in any of the text fields. As long as there was only one text field i didn't have any problem.

<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>

How can i bine the two scripts and pass the data in ajax as an array?
P.S The main reason for scripting is to do a search bining the two input fields. For example if someone enters house, vehicle in requirement field and place1,place2 in area field. The result search should display the result for the following
1) place1 house
2) place1 vehicle
3) place2 house
4) place2 vehicle

Share Improve this question asked Apr 16, 2013 at 10:13 curious_codercurious_coder 2,4684 gold badges28 silver badges45 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

you can set id for each elemnt and get the other's value using

document.getElementById("id1").value

and then make the ajax request

http://jsfiddle/JMpgU/

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

with any number of inputs: http://jsfiddle/JMpgU/2/

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

Try this

<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>

Pass "searchString" as a param.

发布评论

评论列表(0)

  1. 暂无评论