javascript - 无法更改使用所选插件的 html 中的下拉选择

标签 javascript jquery jquery-chosen prototype-chosen

这是我的网络应用程序的 html 片段。 我试图根据 .commtype 的选择来更改 .commmethod 的选定值,但所有常用技术对我都不起作用。

html 片段下面是我的 jquery。

<div class="form-group">
    <div class="col-sm-4">
    <select style="display: none;" class="combobox form-control chzn-done" id="commtype" name="CommunicationTypeId">
         <option value="-1">Select one...</option>
         <option value="401">Phone</option>
         <option value="403">Fax</option>
         <option value="404">Email</option>
         <option value="402">Postal</option>
    </select>
    <div style="width: 513px;" class="chzn-container chzn-container-single" id="commtype_chzn">
        <a href="javascript:void(0)" class="chzn-single" tabindex="-1">
          <span>Select one...</span>
        </a>
        <div class="chzn-drop" style="left: -9000px; width: 511px; top: 43px;">
         <ul class="chzn-results">
              <li id="commtype_chzn_o_0" class="active-result result-selected" style="">Select one...</li>
              <li id="commtype_chzn_o_1" class="active-result" style="">Phone</li>
              <li id="commtype_chzn_o_2" class="active-result" style="">Fax</li>
              <li id="commtype_chzn_o_3" class="active-result" style="">Email</li>
              <li id="commtype_chzn_o_4" class="active-result" style="">Postal</li>
         </ul>
        </div>
     </div>
</div>
<div class="form-group">
    <div class="col-sm-4">
    <select style="display: none;" class="combobox form-control chzn-done" id="commmethod" name="CommunicationMethodId">
        <option value="-1">Select one...</option>
        <option value="2">Slow</option>
        <option value="5">Fast</option>
        <option value="4">Fasted</option>
        <option value="1">Slowest</option>
        <option value="3">Slower</option>
    </select>
    <div style="width: 513px;" class="chzn-container chzn-container-single" id="commmethod_chzn">
      <a href="javascript:void(0)" class="chzn-single" tabindex="-1">
        <span>Select one...</span>
      </a>
      <div class="chzn-drop" style="left: -9000px; width: 511px; top: 43px;">
       <ul class="chzn-results">
           <li id="commmethod_chzn_o_0" class="active-result result-selected" style="">Select one...</li>
           <li id="commmethod_chzn_o_1" class="active-result" style="">Slow</li>
           <li id="commmethod_chzn_o_2" class="active-result" style="">Fast</li>
           <li id="commmethod_chzn_o_3" class="active-result" style="">Fasted</li>
           <li id="commmethod_chzn_o_4" class="active-result" style="">Slowest</li>
           <li id="commmethod_chzn_o_5" class="active-result" style="">Slower</li>
        </ul>
     </div>
 </div>

Jquery 代码确实设置了 Commmethod 的选定值,但是,它不会更改在表单提交验证期间处理条目所需的其他值。 (参见 jquery 之后的代码片段)

<script type="text/javascript">
    $("#commtype").change(function () {
        //I do get the correct selected value here
        var theselected = $('#commtype >option:selected').text();

        switch (theselected) {
            case "Phone":
                $("#commmethod").val(2);
                break; 
            case "Fax":
                $("#commmethod").val(5);
                break;
            case "Email":
                $('#commmethod').val(4);
                break;
            case "Postal":
                $('#commmethod').val(1);
                break;
            default:
                $('#commmethod').val(1);
                break;
        }

        $("#commmethod").trigger("liszt:updated");
    });
</script>

因此,如果我在开关后输出#commmethod.val,我会得到我想要的值,但上面 html 中的以下元素(用 ---> 标记)不会根据需要更新...

<div style="width: 513px;" class="chzn-container chzn-container-single" id="commmethod_chzn">
    <a href="javascript:void(0)" class="chzn-single" tabindex="-1">
--->    <span>Select one...</span>
    </a>
    <div class="chzn-drop" style="left: -9000px; width: 511px; top: 43px;">
       <ul class="chzn-results">
           <li id="commmethod_chzn_o_0" class="active-result result-selected" style="">Select one...</li>
--->       <li id="commmethod_chzn_o_1" class="active-result" style="">Slow</li>
           <li id="commmethod_chzn_o_2" class="active-result" style="">Fast</li>
           <li id="commmethod_chzn_o_3" class="active-result" style="">Fasted</li>
           <li id="commmethod_chzn_o_4" class="active-result" style="">Slowest</li>
           <li id="commmethod_chzn_o_5" class="active-result" style="">Slower</li>
        </ul>
     </div>
 </div>

如果我使用下拉菜单对 Commmethod 进行选择,则上面的内容将更新为该值,并且所选 Commmethod 列表项的类将附加结果选择。

*****更新***** 事实证明,jquery 选择的插件正在对事件进行更新。研究 stackoverflow 以正确更新下拉列表,尚未找到解决方案。

我感谢任何和所有的帮助。谢谢。

最佳答案

我认为您正在寻找类似以下内容的内容。

$("#commtype").change(function () {
    var theselected = $('option:selected', this).text();

    switch (theselected) {
        case "Phone":
            $("#commmethod").val(2);
        case "Fax":
            $("#commmethod").val(5);
        case "Email":
            $("#commmethod").val(4);
        case "Postal":
            $("#commmethod").val(1);
        default:
            $("#commmethod").val(-1);
    }

    $("#commmethod").trigger("liszt:updated"); //to update chosen select
});

关于javascript - 无法更改使用所选插件的 html 中的下拉选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664299/

相关文章:

javascript - 图表JS错误: Uncaught TypeError: Cannot read property 'top' of undefined

javascript - 如何设置多链式选择按钮的样式。?

javascript - jQuery 选择的 Css 问题

javascript - C# 等线程语言上的线程与异步?

javascript - 将 ES7 异步回调绑定(bind)到其父作用域上下文的最简洁方法是什么

javascript - 好的和干净的方法来检查这个? (JS + HTML)

javascript - 同一页面中的多个颜色选择器

jquery - 当所有项目都未选中时,如何取消选中 "select all"复选框?

javascript - 带有表达式的nodejs箭头函数

javascript - 选择下拉列表不显示(jquery-chosen)