📅  最后修改于: 2023-12-03 15:38:03.548000             🧑  作者: Mango
在Web应用程序中,禁用选项选择是非常常见的需求。例如,如果您正在创建一个表单,您可能希望在一些情况下禁用一些选项。使用jQuery Mobile可以轻松实现禁用选项选择。
在您的HTML文件中引入jQuery和jQuery Mobile库。
<!-- jQuery 引入 -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<!-- jQuery Mobile 引入 -->
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
创建一个表单,在表单中添加您想要禁用的选项。
<form>
<label for="option-1">选项 1</label>
<input type="checkbox" name="option-1" id="option-1" value="1">
<label for="option-2">选项 2</label>
<input type="checkbox" name="option-2" id="option-2" value="2">
<label for="option-3">选项 3</label>
<input type="checkbox" name="option-3" id="option-3" value="3">
<label for="option-4">选项 4</label>
<input type="checkbox" name="option-4" id="option-4" value="4">
</form>
要禁用选项选择,请使用 jQuery 的 prop()
函数,将其设置为 disabled
。
// 禁用选项 2
$('#option-2').prop('disabled', true);
为了使禁用的选项看起来与其他选项不同,您可以使用 jQuery Mobile 的 enhanceWithin()
函数来刷新表单。
// 刷新表单,以体现禁用效果
$('form').enhanceWithin();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>使用jQuery Mobile创建禁用选项选择</title>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<form>
<label for="option-1">选项 1</label>
<input type="checkbox" name="option-1" id="option-1" value="1">
<label for="option-2">选项 2</label>
<input type="checkbox" name="option-2" id="option-2" value="2">
<label for="option-3">选项 3</label>
<input type="checkbox" name="option-3" id="option-3" value="3">
<label for="option-4">选项 4</label>
<input type="checkbox" name="option-4" id="option-4" value="4">
</form>
<script>
// 禁用选项 2
$('#option-2').prop('disabled', true);
// 刷新表单,以体现禁用效果
$('form').enhanceWithin();
</script>
</body>
</html>
这样,您就可以使用以上的代码在您的Web应用程序中创建禁用选项选择。