2016-12-05 123 views
1

我使用了带有搜索输入和“添加”按钮的自定义选择框。此输入字段的作用类似于搜索和搜索下拉选项中的值。但是当值与值不匹配时,我需要点击该添加按钮时,该字段的值应该添加到该选项中,并且还充当下拉列表的选定值。请帮助...如何获取输入文本字段在选择框中的值选项值

感谢名单..

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

+0

添加完整的代码至今。我们不知道什么是您的添加按钮和其他细节 – ScanQR

+0

只需点击添加特定的,有一个下拉列表,并在最后它有一个添加按钮..和它的代码是在链接的js文件 –

+0

你能告诉我们什么是代码添加新项目?它是由插件生成的吗? – ScanQR

回答

0

这是解决不了问题,不过这是达成解决方案的方式。

<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 

希望这有助于你.....

0

您需要附加click事件在你的Add New Item按钮上,并点击该按钮,你应该创建一个新的选项,并添加到您的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)); 
}); 
+0

sry man ...它失败了... –

+0

@AnkitJain发布您的更改并在哪里失败? – ScanQR

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

} 
}); 
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>

相关问题