如何在选择框选项值中获取输入文本字段的值

时间:2016-12-05 09:12:30

标签: javascript jquery html

我使用了一个带有搜索输入的自定义选择框和一个"添加"。这个输入字段的行为就像搜索一样,并搜索下拉选项中的值。但是当值与值不匹配时,我需要点击该添加按钮,该字段的值应该添加到选项中,并且还作为下拉列表的选定值。请帮忙......

感谢名单..



$(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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

$('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>');

}
});

答案 1 :(得分:0)

$(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>