使用文本框中的 jquery 将产品名称添加到下拉列表中

Add product names to the dropdownlist using jquery from the text box

本文关键字:产品名 添加 下拉列表 jquery 文本      更新时间:2023-09-26
嗨,我

有一个下拉列表和一个文本框,我想要的是将文本框中写入的产品动态添加到下拉列表中,它也不应该重复值。

我创建了一个带有列 id 和 product 的表[产品],因此添加到下拉列表中的产品应添加到该表中。

关于如何使其工作的任何想法,任何帮助和建议将不胜感激

<div id="dropdown-container">
    <asp:TextBox ID="txtProduct" runat="server"></asp:TextBox><a id="add"
        href="javascript:void(0)">Add</a>
    <select id="product">
    <option>please select</option>
 </select>
</div>

一些可能帮助你入门的代码。如果不了解实际来源,很难帮助您。

在文本框键控事件中:

$(function(){
    $('#add').click(function(){
         var $productTextbox = $('#txtProduct');
         $.get('producthandler.ashx', 
               {  productName: $productTextbox.val() 
                     /* try to make use of 
                     an ID instead of name here */
               })
               .done(function(response){
                   var $option = 
                             $('<option>' + $productTextbox.val() + '</option>');
                   if(response.productDoesNotExist)
                       $('#product').append($option);
               }
         });

    });
});

如果您向我提供实际数据源的外观,我可以为您提供更详细的示例。