【问题标题】:Jquery Mobile Multiple Select List disabled option not working when using .attr("disabled",true) in code在代码中使用 .attr("disabled",true) 时,Jquery Mobile Multiple Select List disabled 选项不起作用
【发布时间】:2017-06-29 20:16:33
【问题描述】:

我正在尝试编写一些 jquery 代码,当我从另一个选择列表中选择一个值时,它将禁用多选列表中的一个选项。该代码会将 disabled 属性添加到多选列表中,但实际上并没有使该选项变灰。我注意到,要让它真正禁用 jquery mobile 中的选项,我必须将类 .ui-state-disabled 添加到无序列表 li 标记中。不过,我不确定如何实现这一目标。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Elite CTA Create Form</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link rel="stylesheet" href="../css/themes/Estimates.css" />
    <link rel="stylesheet" href="../css/themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <script type="application/javascript">
    $(document).on("pagecreate", "#page-form1", function() {
        $("select").each(function(idx) {
            $(this).change(function() {
                var value = $(this).val();
                if($(this).attr("id") == "Priority")
                    linkOptions(value);
            });
        });

        $("#lead").change(function() {
                var value = $(this).val();
                $("#notify option[value='"+value+"']").attr("disabled",true);
                $("#notify").trigger("change");
        });
    });


    function linkOptions(priority) {
        var cases = {

            "Normal": {
                "commETA": "2 Days",
                "compETA": "1 Week"
            },
            "High": {
                "commETA": "Today",
                "compETA": "2 Days"
            },
            "Severe": {
                "commETA": "Today",
                "compETA": "Today"
            },
            "Emergency": {
                "commETA": "1 Hour",
                "compETA": "Today"
            }
        };

        if (cases[priority]) {
            $.each(cases[priority], function(key, value) {
                $("#" + key).val(value).trigger("change");
            });
        }               
    }
    </script>
  </head>

  <body>
    <div data-role="page" id="page-form1" data-theme="a" data-ajax="false">
        <div data-role="panel" id="myPanel">
            <a href="index.php" class="ui-btn ui-corner-all ui-icon-info ui-btn-icon-left" data-ajax="false">View CTA's</a>
            <a href="create.php" class="ui-btn ui-corner-all ui-icon-plus ui-btn-icon-left" data-ajax="false">Create a CTA</a>
        </div> 
        <div data-role="header" data-position="inline">
            <a href="#myPanel" class="ui-btn ui-shadow ui-corner-all ui-icon-bars ui-btn-icon-notext ui-btn-inline"></a>
            <h1>Elite CTA Form</h1>
        </div>
        <div data-role="content" id="page-content1" data-theme="a">
            <form class="form-signin" id="form1" method="post" action="<?php echo $formaction; ?>">
                <h4 class="form-signin-heading" style="text-align: center;">Please describe CTA Below</h4>

                <label style="font-weight: bold;">Customer Name:</label>
                <input name="Customer_Name" id="Customer_Name" type="text" class="form-control" autofocus required="required"><br />
                <label style="font-weight: bold;">Priority Level:</label>   
                <!-- Selected Priority Level will change the Time for Action and Completion Field Automatically JS Above -->
                <select name="Priority" id="Priority" class="selectnoreset" required>
                    <option value="">Please Select</option>
                    <option value="Normal">Normal</option>
                    <option value="High">High</option>
                    <option value="Severe">Severe</option>
                    <option value="Emergency">Emergency</option>
                </select><br />
                <label style="font-weight: bold;">Project:</label>
                <input name="Project" id="Project" type="text" class="form-control"><br />
                <label style="font-weight: bold;">Expected Time for Action:</label>
                <select name="commETA" id="commETA" class="selectnoreset" required>
                    <option value="">Please Select</option>
                    <option value="1 Hour">1 Hour</option>
                    <option value="Today">Today</option>
                    <option value="2 Days">2 Days</option>
                    <option value="1 Week">1 Week</option>
                    <option value="2 Weeks">2 Weeks</option>
                </select><br />
                <label style="font-weight: bold;">Expected Time for Completion:</label>
                <select name="compETA" id="compETA" class="selectnoreset" required>
                    <option value="">Please Select</option>
                    <option value="Today">Today</option>
                    <option value="2 Days">2 Days</option>
                    <option value="1 Week">1 Week</option>
                    <option value="2 Weeks">2 Weeks</option>
                    <option value="1 Month">1 Month</option>
                    <option value="Unknown">Unknown</option>
                </select><br />
                <label style="font-weight: bold;">Description of Issue:</label>
                <textarea name="issue" id="issue" required></textarea><br />
                <label style="font-weight: bold;">Unknowns About Issue:</label>
                <textarea name="unknowns" id="unknowns"></textarea><br />
                <label style="font-weight: bold;">People Involved in CTA:</label>
                <textarea name="people" id="people"></textarea>
                <!-- Assign Main Contact -->
                <select name="lead" id="lead" data-iconpos="left" required>
                    <option value="Mark">Mark</option>
                    <option value="Dan">Dan</option>
                    <option value="Alex">Alex</option>
                    <option value="Brian">Brian</option>
                </select>
                <!-- End Main Contact -->
                <!-- Assign Viewers to the CTA -->
                <select multiple="multiple" data-native-menu="false" name="notify[]" id="notify" data-iconpos="left" required>
                    <option>Assign Viewers</option>
                    <option value="Mark">Mark</option>
                    <option value="Dan">Dan</option>
                    <option value="Alex">Alex</option>
                    <option value="Brian">Brian</option>
                </select>
                <!-- End Viewers -->
                <br /><br />
                <input type="hidden" name="doform" value="doform">
                <button name="Submit" id="submit" class="btn btn-lg btn-primary btn-block" type="submit">Submit CTA</button>

                <div id="message"></div>
            </form>

        </div> <!-- /container -->
  </body>
</html>

---编辑下面的工作jQuery代码 我最终将 deblocker 的代码与我的混合使用,并最终得到以下代码。这确实像我现在所期望的那样工作。因此,当我在潜在客户选择列表中选择一个名称时,它会使通知选择列表的多选中的选项变灰。选择不同的用户将清除先前选择的用户,并仅显示已选择的用户。再次感谢 deblocker 的出色帮助!

$("#lead").change(function() {
    var value = $(this).val();
    $("#notify option").removeAttr("disabled");
    $("#notify option[value='"+value+"']").attr("disabled",true);
    $("#notify").selectmenu("refresh", true);
});

【问题讨论】:

    标签: javascript jquery jquery-mobile


    【解决方案1】:

    触发器更改确实会影响该值,要禁用一个选项,您应该刷新小部件。试试这个:

    function toggleOptions() {
      var value = "Mark";
      if($("#select-custom option[value='"+value+"']").attr("disabled"))
        $("#select-custom option[value='"+value+"']").removeAttr("disabled")
      else
        $("#select-custom option[value='"+value+"']").attr("disabled","disabled");
      $("#select-custom").selectmenu("refresh", true);
    }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
      <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
      <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
      <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    </head>
    <body>
      <div data-role="page" id="page1">
        <div data-role="content">
          <button class="ui-btn ui-corner-all ui-mini" onclick="toggleOptions();">toggle Mark</button>
          <div class="ui-field-contain">
            <label for="select-custom">Assign Viewers</label>
            <select name="select-custom" id="select-custom" multiple="multiple" data-native-menu="false" data-mini="true">
              <option value="Mark">Mark</option>
              <option value="Dan">Dan</option>
              <option value="Alex">Alex</option>
              <option value="Brian">Brian</option>
            </select>
          </div>
        </div>
      </div>
    </body>
    
    </html>

    【讨论】:

    • 再次感谢 deblocker 我让它与您的 removeAttr 和 selectmenu("refresh", true) 代码示例一起使用!你的救命稻草。
    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    相关资源
    最近更新 更多