在 jQuery Mobile Controlgroup 中选中一个时如何取消选中其他人?
How uncheck others when check one in jQuery Mobile Controlgroup?
我的目标是在选中一个复选框项目时取消选中其他项目。这是不适用于 jquery mobile 1.4.2 ...
的代码
JavaScript:
$('input.answer').on('change', function() {
$('input.answer').not(this).prop('checked', false);
});
HTML:
<form>
<fieldset data-role="controlgroup">
<legend for="0-0">Uncheck others when check one of the items</legend>
<label for="0-0-4">one</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-4" id="0-0-4" value="4">
<label for="0-0-3">two</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-3" id="0-0-3" value="3">
<label for="0-0-2">three</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-2" id="0-0-2" value="2">
<label for="0-0-1">four</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-1" id="0-0-1" value="1">
<label for="0-0-0">five</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-0" id="0-0-0" value="0">
</fieldset>
</form>
如果您正在使用 jquery UI,只需添加 checkboxradio 函数...
$('input.answer').not(this).prop('checked', false).checkboxradio("refresh");
祝你有愉快的一天
因为 jQuery Mobile 将自己的样式添加到每个 Checkbox
- 也添加到整个 Controlgroup
- 您应该调用 refresh
方法 Controlgroup
其中包含您更改的元素。请注意,您应该在 pagecreate
事件中添加事件侦听器。
$(document).on("pagecreate", "#page-one", function() {
$('input.answer').on('change', function() {
$('input.answer').not(this).prop('checked', false);
$(this).closest("fieldset").controlgroup("refresh"); // add this line
/* Display the result */
var result = "";
$("input.answer").each(function(index) {
var caption = $(this).parent().find("label").text();
var checked = $(this).prop('checked');
result += caption + ":" + checked + " ";
});
$(this).closest("fieldset").find("legend").html(result);
});
});
<!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.js"></script>
</head>
<body>
<div id="page-one" data-role="page">
<div role="main" class="ui-content">
<form>
<fieldset data-role="controlgroup" data-mini="true">
<legend for="0-0">Uncheck others when check one of the items</legend>
<label for="0-0-4">one</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-4" id="0-0-4" value="4">
<label for="0-0-3">two</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-3" id="0-0-3" value="3">
<label for="0-0-2">three</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-2" id="0-0-2" value="2">
<label for="0-0-1">four</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-1" id="0-0-1" value="1">
<label for="0-0-0">five</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-0" id="0-0-0" value="0">
</fieldset>
</form>
</div>
</div>
</body>
</html>
我的目标是在选中一个复选框项目时取消选中其他项目。这是不适用于 jquery mobile 1.4.2 ...
的代码JavaScript:
$('input.answer').on('change', function() {
$('input.answer').not(this).prop('checked', false);
});
HTML:
<form>
<fieldset data-role="controlgroup">
<legend for="0-0">Uncheck others when check one of the items</legend>
<label for="0-0-4">one</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-4" id="0-0-4" value="4">
<label for="0-0-3">two</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-3" id="0-0-3" value="3">
<label for="0-0-2">three</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-2" id="0-0-2" value="2">
<label for="0-0-1">four</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-1" id="0-0-1" value="1">
<label for="0-0-0">five</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-0" id="0-0-0" value="0">
</fieldset>
</form>
如果您正在使用 jquery UI,只需添加 checkboxradio 函数...
$('input.answer').not(this).prop('checked', false).checkboxradio("refresh");
祝你有愉快的一天
因为 jQuery Mobile 将自己的样式添加到每个 Checkbox
- 也添加到整个 Controlgroup
- 您应该调用 refresh
方法 Controlgroup
其中包含您更改的元素。请注意,您应该在 pagecreate
事件中添加事件侦听器。
$(document).on("pagecreate", "#page-one", function() {
$('input.answer').on('change', function() {
$('input.answer').not(this).prop('checked', false);
$(this).closest("fieldset").controlgroup("refresh"); // add this line
/* Display the result */
var result = "";
$("input.answer").each(function(index) {
var caption = $(this).parent().find("label").text();
var checked = $(this).prop('checked');
result += caption + ":" + checked + " ";
});
$(this).closest("fieldset").find("legend").html(result);
});
});
<!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.js"></script>
</head>
<body>
<div id="page-one" data-role="page">
<div role="main" class="ui-content">
<form>
<fieldset data-role="controlgroup" data-mini="true">
<legend for="0-0">Uncheck others when check one of the items</legend>
<label for="0-0-4">one</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-4" id="0-0-4" value="4">
<label for="0-0-3">two</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-3" id="0-0-3" value="3">
<label for="0-0-2">three</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-2" id="0-0-2" value="2">
<label for="0-0-1">four</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-1" id="0-0-1" value="1">
<label for="0-0-0">five</label>
<input class="answer answer-num-6" type="checkbox" name="0-0-0" id="0-0-0" value="0">
</fieldset>
</form>
</div>
</div>
</body>
</html>