Jquery 移动多个 Select 在代码中使用 .attr("disabled",true) 时列表禁用选项不起作用
Jquery Mobile Multiple Select List disabled option not working when using .attr("disabled",true) in code
我正在尝试编写一些 jquery 代码,当我从另一个 select 列表中选择一个值时,这些代码将禁用多个 select 列表中的一个选项。该代码会将 disabled 属性添加到 select 多重列表,但它实际上并没有使该选项变灰。我注意到,要真正禁用 jquery mobile 中的选项,我必须将 class .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 的代码和我的代码,并得到了以下代码。这确实像我现在期望的那样工作。因此,当我在 lead select 列表中选择一个名称时,它会将通知 select 列表的多个 select 中的选项变灰。选择不同的用户将清除之前 selected 的用户,并使 selected 的用户变灰。再次感谢 deblocker 的精彩帮助!
$("#lead").change(function() {
var value = $(this).val();
$("#notify option").removeAttr("disabled");
$("#notify option[value='"+value+"']").attr("disabled",true);
$("#notify").selectmenu("refresh", true);
});
触发器更改确实会影响值,要禁用一个选项,您应该刷新小部件。试试这个:
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>
我正在尝试编写一些 jquery 代码,当我从另一个 select 列表中选择一个值时,这些代码将禁用多个 select 列表中的一个选项。该代码会将 disabled 属性添加到 select 多重列表,但它实际上并没有使该选项变灰。我注意到,要真正禁用 jquery mobile 中的选项,我必须将 class .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 的代码和我的代码,并得到了以下代码。这确实像我现在期望的那样工作。因此,当我在 lead select 列表中选择一个名称时,它会将通知 select 列表的多个 select 中的选项变灰。选择不同的用户将清除之前 selected 的用户,并使 selected 的用户变灰。再次感谢 deblocker 的精彩帮助!
$("#lead").change(function() {
var value = $(this).val();
$("#notify option").removeAttr("disabled");
$("#notify option[value='"+value+"']").attr("disabled",true);
$("#notify").selectmenu("refresh", true);
});
触发器更改确实会影响值,要禁用一个选项,您应该刷新小部件。试试这个:
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>