【问题标题】:Autocomplete ajax is not working自动完成 ajax 不起作用
【发布时间】:2015-05-06 16:47:30
【问题描述】:
        <script>

function autocomplet1() {
    var min_length = 0; // min caracters to display the autocomplete
    var keyword = $('#select01').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'barcode.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#barcode01').show();
                $('#barcode01').html(data);
            }
        });
    } else {
        $('#barcode01').hide();
    }
}

function set_item(item2) {
    $('#select01').val(item2);
    var customer = $("#customer").val();
    $.post("sales/add", {"data[Sales][item]": item2 ,"data[Sales][customer_phone]": customer  }, function (data) {
      $('#details3').html(data);
       $("#select01").val('');
});
    // hide proposition list
    $('#barcode01').hide();
}
</script>

<script>
// autocomplet : this function will be executed every time we change the text
function autocomplet() {
    var min_length = 0; // min caracters to display the autocomplete
    var keyword = $('#customer').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'abc.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#country_list_id').show();
                $('#country_list_id').html(data);
            }
        });
    } else {
        $('#country_list_id').hide();
    }
}
function set_item(item1) {
    $('#customer').val(item1);
    $.post("sales/add", {"data[Sales][customer]": item1 }, function (data) {
      $('#details').html(data);
});
    // hide proposition list
    $('#country_list_id').hide();
}
</script>


 <div class="input_container">


             <input type="text"  id='customer' onkeyup="autocomplet()" name="data[Sales][customer]" class="input-small focused">
                    <ul id="country_list_id" style='height: 500px;'></ul></div></div>
<div class="content">
         <div class="input_container">


             <input type="text"  id='select01' onkeyup="autocomplet1()" name="data[Sales][item]" class="input-small focused">
                    <ul id="barcode01" style='height: 500px;'></ul></div></div>

上面是一个自动完成脚本。两个脚本具有相同的功能 set_time 。如果我将函数名称更改为 set_time1,则自动完成功能不起作用。如何解决这个问题? .我需要在一个页面中使用两个自动完成功能。

【问题讨论】:

  • 我在您的脚本中看不到 set_timeset_time1 函数。但是你声明了两次同名的函数function set_item,第二次覆盖第一个。所以第一个不能被调用。
  • 实际上两个脚本具有相同的功能 set_item 。现在告诉我如何更改它。总共有两个函数 set_time 。这就是脚本无法正常工作的原因
  • 这不是“两个脚本”,因为它们都是同一个页面的一部分,所以它们都是同一个脚本。两个具有相同名称的函数不能在 javascript 中工作,因为第二个定义将覆盖第一个。请给我看“set_time”功能,因为你确认它在这里,我确认我找不到它。
  • 我的问题和这个一样。stackoverflow.com/questions/27389241/…
  • 很好,一个副本。如果您阅读它,我的答案是完全一样的。

标签: javascript jquery ajax autocomplete jquery-autocomplete


【解决方案1】:

好的,因为这在小cmets中很难解释,我就写在这里。

拿那个脚本:

function set_item(){
   alert("Hello!");
}

function set_item(){
   alert("Goodbye!");
}

set_item();

你认为会发生什么?什么会被提醒? 答案:“再见!”,因为第二个函数定义覆盖了第一个。你永远不会看到“你好!”警告。

当两个函数具有相同名称时会发生这种情况。你有两个同名的函数。将它们放在不同的 &lt;script&gt; 标签中不会改变任何东西,它仍然是相同的范围。

【讨论】:

  • 再见警报正在显示。但我需要根据 id 显示。如果 id 是客户 set_item 功能不同。如果是 select01 id 那么 set_item 函数是不同的。如何为此编写查询。
  • 写两个函数并给他们一个不同的名字!我不知道如何更好地解释它,是不是很难理解?抱歉,我的论点已经用完了
猜你喜欢
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2012-07-10
相关资源
最近更新 更多