简体   繁体   English

在两个输入框中自动完成? -JavaScript

[英]Autocomplete in two input boxes? - javascript

Hi i am trying to find out how to use my javascript code for two input boxes. 嗨,我正在尝试找出如何将我的JavaScript代码用于两个输入框。 the data its grabing and the URL is using C# code but that should not matter. 抓取的数据和URL使用C#代码,但这无关紧要。 Please help me with my autocomplete.. 请帮我完成自动填充。

Here is a test page of it in action 这是运行中的测试页

http://www.bivar.com/test.aspx http://www.bivar.com/test.aspx

Here is the code i am using for the javascript. 这是我用于javascript的代码。

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>  
<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
             select:function(event, ui){
              window.location.href = '/Products/ProductInfoCenter.aspx?partnum=' + ui.item.value;
           },
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/test.aspx/GetAutoCompleteData",
                    data: "{'PartNumber':'" + document.getElementById('txtPartNum').value + "'}",
                    dataType: "json",
                    success: function (data) {
                       response(data.d);
                    },
                    error: function (result) {
                        alert(err.message);
                    }
                });
            }
        });
    }
</script>

Here are the two input boxes 这是两个输入框

   <input type="text" id="txtPartNum" class="autosuggest" />
      <input type="text" id="txtPartNum2" class="autosuggest" />

Thank you and please help. 谢谢,请帮忙。

Here is your function updated. 这是您的功能更新。 Tested and its working perfectly. 经过测试,其工作完美。 Problem was that, it was passing value of first input box every time. 问题是,它每次都在传递第一个输入框的值。 I used $(this.element) to get current element on which autocomplete is requested. 我使用$(this.element)来获取当前要求自动完成的元素。 When dealing with classes we have to make use of (this) keyword to avoid conflicts. 在处理类时,我们必须使用(this)关键字以避免冲突。

function SearchText() {
  $(".autosuggest").autocomplete({
     select:function(event, ui){
         window.location.href = '/Products/ProductInfoCenter.aspx?partnum=' + ui.item.value;
      },
    source: function (request, response) { 
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/test.aspx/GetAutoCompleteData",
            data: "{'PartNumber':'" + $(this.element).val() + "'}",
            dataType: "json",
            success: function (data) {
               response(data.d);
            },
            error: function (result) {
                alert(err.message);
            }
        });
    }
  });
}

The only problem I see is that both the inputs are sending same data as parameter. 我看到的唯一问题是两个输入都发送相同的数据作为参数。 But that is what it supposed to do isn't it? 但这是应该做的吗? Because you are using this - 因为您使用的是-

data: "{'PartNumber':'" + document.getElementById('txtPartNum').value + "'}",

in your code. 在您的代码中。 I think this is the error. 我认为这是错误。 But please make sure you ask properly what you are looking for. 但请确保您正确询问要寻找的内容。 Your question is not completely understandable. 您的问题不是完全可以理解的。 A usual code should look like this - 常规代码应如下所示-

data: "{'PartNumber':'" + request.term + "'}",

Let me know if this what you were looking for. 让我知道您在寻找什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM