【问题标题】:How to set Id and name with auto complete如何使用自动完成设置 ID 和名称
【发布时间】:2013-10-20 07:46:14
【问题描述】:

在行动课

public class Supplier extends ActionSupport{

        private ArrayList supplierList;
       //contains list of supplisers with attribute(id,name,address,mailid)
      //getter and setter of supplierList
    }

这是我的表单(在 Jsp 中)

<form action="test" method="post">
   <input type="text" name="supplierName" id="supplierName">
    <input type="hidden" name="supplierId" id="supplierId">
</form>

我想显示供应商列表名称的自动完成功能,根据所选名称,我想在文本和隐藏框中设置它的 ID 和名称,在提交表单时我想发送元素的 ID

【问题讨论】:

  • 我只能在 jsp 页面上显示自动完成

标签: jquery autocomplete


【解决方案1】:

您必须在自动完成的“选择”事件中设置隐藏字段的值,以便当用户从自动完成建议列表中选择名称时,隐藏字段设置为该名称的 id:

$('#textbox-id').autocomplete({
source: // Your ajax call here.

select: function(event, ui){
$('#supplierId').val(ui.item.id);
}
});

http://jqueryui.com/autocomplete/#remote

【讨论】:

    【解决方案2】:

    您应该使用 jquery 定义一个 live 方法,以便任何时候单击自动完成元素之一来设置输入

    <script type="text/javascript">
    $('div.supplierItem').live('click', function(){
       $('#supplierId').val($(this).attr('id'));
       $('#supplierName').val($(this).val());
    });
    </script>
    
    <div class="autoComplete">
       <div class="supplierItem" name="aaa" id="1">
          value1
       </div>
       <div class="supplierItem" name="bbb" id="2">
         value2
       </div>
    </div>
    

    【讨论】:

      【解决方案3】:

      也许对你有帮助: 1)将属性类设置为您的自动完成, 2) 使用类名查找自动完成

       $( elem ).autocomplete({
      source: ...}).autocomplete( "widget" ).addClass( "whatever" );
      

      来自http://bugs.jqueryui.com/ticket/8928

      【讨论】:

        猜你喜欢
        • 2015-01-16
        • 1970-01-01
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 2019-05-01
        • 1970-01-01
        • 2011-07-05
        相关资源
        最近更新 更多