【问题标题】:How to get value of input text field in select box option value如何在选择框选项值中获取输入文本字段的值
【发布时间】:2016-12-05 09:12:30
【问题描述】:

我使用了一个带有搜索输入和“添加”按钮的自定义选择框。这个输入字段的作用类似于搜索和搜索下拉选项中的值。但是当值与值不匹配时,我需要单击该添加按钮时,该字段的值应添加到选项中,并充当下拉列表的选定值。请帮忙...

谢谢..

$(function() {
        $(".standards").customselect();
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='http://classvita.com/admin/res/javascripts/jquery-customselect.js'></script>
<link href='http://classvita.com/admin/res/stylesheets/jquery-customselect.css' rel='stylesheet' />
<select name='standard' class='custom-select standards'>
    <option value=''>Add Particular</option>
    <option value='1'>abc</option>
   <option value='2'>aabc</option>
   <option value='3'>abbc</option>
</select>

【问题讨论】:

  • 添加你的完整代码到现在。我们不知道您的添加按钮和其他详细信息是什么
  • 只需点击添加特定,有一个下拉菜单,最后有一个添加按钮..它的代码在链接的js文件中
  • 你能告诉我们添加新项目的代码是什么吗?是插件生成的吗?

标签: javascript jquery html


【解决方案1】:

这不是解决方案,但这是获得解决方案的一种方式。

<select name='standard' class='custom-select standards' id="SelectList">  //add id attribute
<option value=''>Add Particular</option>
<option value='1'>abc</option>
<option value='2'>aabc</option>
<option value='3'>abbc</option>
</select>

这是添加选择列表项的方法:

JS:

var html = "<option value=\""+ ListItemNumber +"\">"+ TheSearchTerm +"</option>";
$('#SelectList').append(html);   //binding the html
$('#SelectList').val(ListItemNumber);   //Select the ListItem

希望对你有帮助.....

【讨论】:

    【解决方案2】:

    您需要在Add New Item 按钮上附加click 事件,然后单击该按钮,您应该创建一个新选项并添加到您的custom 选择中,如下所示,

    $(document).on('click', "#add_new_button_id", function(){
      //get the value from input text
       var typedValue = $("#input_id").val();
      //create option on click and add to your custom select
      $('.custom-select').append($("<option></option>").attr("value", typedValue).text(typedValue));
    });
    

    【讨论】:

    • @AnkitJain 发布您的更改以及失败的地方?
    【解决方案3】:
    $('body').on('change','select[name="standard"]',function(){
    
    var pid = $(this).val(); //grabbing the current value of the select.
    if(check here if pid lies in some sort of arrays ,values etc)
    {
    $('select[name="standard"]').append('<option value="'+pid+'">'+pid+'</option>');
    
    }
    });
    

    【讨论】:

      【解决方案4】:

      $(function() {
              $(".standards").customselect();
            });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src='http://classvita.com/admin/res/javascripts/jquery-customselect.js'></script>
      <link href='http://classvita.com/admin/res/stylesheets/jquery-customselect.css' rel='stylesheet' />
      <select name='standard' class='custom-select standards'>
          <option value=''>Add Particular</option>
          <option value='1'>abc</option>
      
         <option value='2'>aabc</option>
         <option value='3'>abbc</option>
      </select>

      【讨论】:

        猜你喜欢
        • 2013-08-27
        • 1970-01-01
        • 2023-03-24
        • 2014-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-30
        相关资源
        最近更新 更多