我试图创建一个在选择某些内容时有效的脚本,将选中一个复选框,并禁用一些复选框。这是我的代码:
<script type="text/javascript">
$(document).ready(function () {
$('#myselect').change(function(){
if (!$(this.val() == "0")){
$('#boss').prop('checked', true);
$('#manager').attr('disabled', true);
$('#crew').attr('disabled', true);
}
});
})
</script>
<div>
<%= f.label :company_code, "Company:" %>
<select id="myselect">
<option value = "0">Mcdo</option>
<option value = "5">Burger King</option>
<option value = "1">KFC</option>
</select>
</div>
<div>
Select Role:
</div>
<br />
<div>
<p><%= f.check_box :boss, :id => 'boss' %>Boss</p>
<p><%= f.check_box :manager, :id => 'manager' %>Manager</p>
<p><%= f.check_box :crew, :id => 'crew' %>Crew</p>
</div>
答案 0 :(得分:1)
$(document).ready(function() {
$('#myselect').change(function() {
//you have an error here in closing the bracket, also the condition is not proper
if ($(this).val() != "0") {
$('#boss').prop('checked', true);
$('#manager').prop('disabled', true);
$('#crew').prop('disabled', true);
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="myselect">
<option value="0">Mcdo</option>
<option value="5">Burger King</option>
<option value="1">KFC</option>
</select>
<input type="checkbox" id="boss" />
<input type="checkbox" id="manager" />
<input type="checkbox" id="crew" />
您可能还必须处理必须启用这些输入的情况,可能类似于
$(document).ready(function() {
$('#myselect').change(function() {
var state = $(this).val() == "0";
if (!state) {
$('#boss').prop('checked', true);
}
$('#manager, #crew').prop('disabled', !state);
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="myselect">
<option value="0">Mcdo</option>
<option value="5">Burger King</option>
<option value="1">KFC</option>
</select>
<input type="checkbox" id="boss" />
<input type="checkbox" id="manager" />
<input type="checkbox" id="crew" />
答案 1 :(得分:0)
此行if (!$(this.val() == "0")){
中存在语法错误,并将其更改为if (!$(this).val() == "0") {
。