dot_0620 2019-07-16 14:18 采纳率: 100%
浏览 59

具有多种输入类型的自动完成功能,动态插入

I'm trying to make an autocomplete using sql, php, javascript, using dinamically inserted lines on a table.

The point is that the autocomplete is working for the first line but not working for the lines I add after.

My HTML to add lines:

<table class='ui striped table inverted'>
   <tr align="center">
      <td>Contacto</td>
   </tr>
   <tr align="center" class="linhas">
      <td>
         <div class="ui input">
            <input type="text" id = "contacto" />
         </div>
      </td>
      <td><a href="#" class="removerCampo" title="Remover linha">Remover</a></td>
   </tr>
</table>
<tr>
   <td colspan="2"><a href="#" class="adicionarCampo" title="Adicionar item">Adicionar linha</a></td>
</tr>

My Javascript script for the add lines dinamically:

$(function () {
           function removeCampo() {
            $(".removerCampo").unbind("click");
            $(".removerCampo").bind("click", function () {
               if($("tr.linhas").length > 1){
                $(this).parent().parent().remove();
               }
            });
           }

           $(".adicionarCampo").click(function () {

            novoCampo = $("tr.linhas:first").clone();  

            novoCampo.find("input").val("");

            novoCampo.insertAfter("tr.linhas:last");

            removeCampo();
           });
         });
      </script>

My Javascript script for the autocomplete:

<script type="text/javascript">
$(document).ready(function() {

      function log( message ) {
            $( "<div/>" ).text( message ).prependTo( "#log" );
            $( "#log" ).scrollTop( 0 );
      }

      $("#contacto").autocomplete({
            source: "searchContactos.php",
            minLength: 3,
            select: function(event,ui) {
                  log(ui.item ?
                        "Selected: " + ui.item.value :
                        "Nothing selected input was " + this.value);
            }
      });


});
</script>

My php file:

if (isset($_GET['term'])){
  $autoCompleteResult = array();

  $tsql = "SELECT no_, name FROM [Master].[dbo].[Contactos Portal] WHERE name 
           LIKE '$term' or no_ LIKE '$term' ";
  $result = sqlsrv_query($conn, $tsql);

  while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
     array_push($autoCompleteResult,array("value" => $row['name'] .' -> '. 
     $row['no_'] ));
  }

sqlsrv_close($conn);
echo json_encode($autoCompleteResult);
}

I expected the autocomplete to work for as many lines as I inserted. Only working for the first line. Can someone please point me the right direction?

This is the process. The autocomplete only works on the first line on square #1. On #4 it does not work.

Process

Thank you very much.

  • 写回答

1条回答 默认 最新

  • duannaiying9662 2019-07-30 12:58
    关注

    I see two things:

    1. You are selecting your input with an ID, implying that there should only be one. But because you expect the user to add several #Contacto, I would say assigning a class .Contacto (and selecting on that) makes more sense.
    2. When you clone() your input box, you aren't including the event listener you attached to the first one. clone() has an overload that lets you include those attached event listeners. You should be able to use clone(true) to copy not just the input, but the autocomplete functionality as well.
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题