也许可以在这里有人帮助我。我有这种情况:一个选择框与severa选项,我想改变选择的选项,取决于为用户在文本框中引入的选项值。到目前为止,我没有获得成功,我尝试了一切,但总是在我选择了它仍然被选中并且不想失去的东西之后。我的选择框是这样在选择框中选择并取消选择多个选项
<select id="slt-np" class="form-control">
<option selected="" value="0">Selects one</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
和jQuery的处理程序代码如下所示:
$("#textbox").keypress(function(e) {
if (e.which == 13) {
var selectedoption = $("#cont").children("#slt");//This is to take the select
$(selectedoption).find("option[value='"+$("#textbox").val()+"']").prop("selected",true); //select the option with value in textbox
if ($(selectedoption).find("option[value='" + $("#npip").val() + "']").prop("selected") == false) { //if this option have been not selected
$(selectedoption).find("option[value='" + $("#npip").val() + "']").prop("selected", true); //I select it now
} else {
$(selectedoption).find("option[value='" + $("#npip").val() + "']").prop("selected", false); //if was selected I deselect it
}
你可以把这个添加到jsfiddle吗? – deepakb
用户 - 他们是否应该输入文本或数值(即他们将输入“1”还是“1”) – Taleeb