我需要一个javascript代码,只有当用户点击<option value="0" selected="true">Hide a1 + a4</option>
<form action="" method="POST">
<select class="required-entry">
<option value="0" selected="true">Hide a1 + a4</option>
<option value="2">WhatsApp only</option>
<option value="1">E-mail only</option>
<option value="3">WhatsApp + E-mail</option>
</select>
<label ><input type="checkbox" name="a1" checked="">1</label><br />
<label ><input type="checkbox" name="a2" checked="">2</label><br />
<label ><input type="checkbox" name="a3" checked="">3</label><br />
<label ><input type="checkbox" name="a4" checked="">4</label><br />
<label ><input type="checkbox" name="a5" checked="">5</label><br />
<input type="submit" />
</form>
这是我使用的JavaScript:
$(function() {
$('.required-entry').change(function(){ // use class or use $('select')
if ($(this).val() == "0") {
document.getElementById("id of checkbox").checked = false;
} else {
}
});
});
答案 0 :(得分:2)
尝试如下,
在html中......
<form action="" method="POST">
<select class="required-entry">
<option value="0" selected="true">Hide a1 + a4</option>
<option value="2">WhatsApp only</option>
<option value="1">E-mail only</option>
<option value="3">WhatsApp + E-mail</option>
</select>
<label ><input type="checkbox" name="a1" id="a1" checked="">1</label><br />
<label ><input type="checkbox" name="a2" id="a2" checked="">2</label><br />
<label ><input type="checkbox" name="a3" id="a3" checked="">3</label><br />
<label ><input type="checkbox" name="a4" id="a4" checked="">4</label><br />
<label ><input type="checkbox" name="a5" id="a5" checked="">5</label><br />
<input type="submit" />
</form>
在javascript中......
$(function() {
$('.required-entry').change(function(){ // use class or use $('select')
if ($(this).val() == "0") {
document.getElementById("a1").checked = false;
document.getElementById("a4").checked = false;
} else {
}
});
});
答案 1 :(得分:1)
这是一个jQuery解决方案:
$(function () {
$('.required-entry').change(function () {
if ($('select option:selected').val() == 0) {
$('input[name="a1"]').prop('checked', false)
$('input[name="a4"]').prop('checked', false)
}
});
});
答案 2 :(得分:0)
您可以尝试这样做,这样您就不必添加ID了。
$(function() {
$('.required-entry').change(function(){
if ($(this).val() == "0") {
document.getElementsByName("a1").checked = false;
document.getElementsByName("a4").checked = false;
}
});
});
答案 3 :(得分:0)
这适用于您当前的HTML和jQuery。
$('.required-entry').change(function(e) {
if (e.target.value === '0') {
$("input[name='a1']")[0].checked = false;
$("input[name='a4']")[0].checked = false;
}
});
答案 4 :(得分:0)
这是正确的代码。
$(function () {
$('.required-entry').change(function () {
if ($(this).val() == "0") {
$('input[name="waalertdown"]').prop('checked', false)
}
});
});