简体   繁体   English

如何使用JQuery用您自己的选项替换下拉菜单选项?

[英]How to replace dropdown menu options with your own options using JQuery?

I have a dropdown menu: Code. 我有一个下拉菜单:代码。 The options of "code" come from the database and the dropdown is populated. “代码”的选项来自数据库,并填充了下拉列表。 I have a textbox: Name. 我有一个文本框:名称。 For certain values populated in Name (again from database), I want to replace the dropdown menu options with my own set of options, essentially overwriting the ones that are already there. 对于“名称”中填充的某些值(再次从数据库中),我想用我自己的选项集替换下拉菜单选项,实质上是覆盖那些已经存在的选项。 Is there a way to do this? 有没有办法做到这一点?

My Code: HTML: 我的代码:HTML:

 <div>
    <table>
    <tbody><tr><td><label>Code</label></td><td>
    <select class="codeSelection"></select></td></tr>
    </tbody>
    </table>                            
</div>

<div>
 <table>
  <tbody><tr><td><label>Name</label></td>
    <td><input type="text" class="nameInput"></td></tr>
  </tbody>
 </table>                            
</div>

Code that I tried so far: (followed How do I add options to a DropDownList using jQuery? ) 到目前为止,我尝试过的代码:(以下如何使用jQuery向DropDownList添加选项?

$('.nameInput').html("");
$('.nameInput').val(data[0].codeValue);
    if ($('.nameInput').val() =="Smith")
            {
                var myOptions = {
                    val1 : "Tom",
                    val2 : "John"
                            };
                    $.each(myOptions, function(val, text){
                    $('.codeSelection').append($('<option></option>').val(val).html(text));
                            });
            }

This still gives me the old dropdown and just displays the values "Tom" and "John" as text. 这仍然给了我旧的下拉菜单,仅将“ Tom”和“ John”值显示为文本。

The below code will read your text box value and loop thru the options inside the dropdown and update as needed. 以下代码将读取您的文本框值并遍历下拉菜单中的选项,并根据需要进行更新。 Make changes as needed. 根据需要进行更改。

  var txtVal=$("#nameInput").val();

  $("#codeSelection option").each(function() {
    var _item=$(this);  
    console.log(_item.val());
    if(_item.val()===txtVal)
     {
         //_item is the one you want. Make the changes as needed.
         // Here i am updating the value and text.
         _item.val("newValue").text("NewName");
     }
   });

Working sample : http://jsbin.com/zexapizabe/2/ 工作示例: http : //jsbin.com/zexapizabe/2/

You just need to clear your options before appending: 您只需要在添加之前清除您的选项:

$('.codeSelection option').remove();

There's a runnable snippet below if you want to see it in action. 如果您想查看实际运行的代码段,请在下面找到。 As soon as you click off the name, the dropdown options will change. 单击名称后,下拉选项将更改。

 $('.nameInput').on("blur", function() { if ($('.nameInput').val() =="Smith") { var myOptions = { val1 : "Tom", val2 : "John" }; $('.codeSelection option').remove(); $.each(myOptions, function(val, text){ $('.codeSelection').append($('<option></option>').val(val).html(text)); }); } }); $(function() { $(".nameInput").get(0).focus(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <table> <tbody><tr><td><label>Code</label></td><td> <select class="codeSelection"> <option>Name One</option> </select></td></tr> </tbody> </table> </div> <div> <table> <tbody><tr><td><label>Name</label></td> <td><input type="text" class="nameInput" value="Smith"></td></tr> </tbody> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM