Javascript 当我运行这段代码时,它说函数计数未定义
我希望当我点击复选框时,其上的值将显示在下面的下拉列表中。请帮帮我。多谢各位Javascript 当我运行这段代码时,它说函数计数未定义,javascript,jquery,Javascript,Jquery,我希望当我点击复选框时,其上的值将显示在下面的下拉列表中。请帮帮我。多谢各位 window.onload = function countChecked() { var n = $("input.theme:checked").length; if (n == 1) { $('input.theme:checkbox:not(:checked)').prop('disabled', true); } else { $('input.theme:checkbox:not
window.onload = function countChecked() {
var n = $("input.theme:checked").length;
if (n == 1) {
$('input.theme:checkbox:not(:checked)').prop('disabled', true);
} else {
$('input.theme:checkbox:not(:checked)').prop('disabled', false);
}
}
$("input.theme:checkbox").click(countChecked);
$('.Go').click(function() {
var i=1;
$('input[type="checkbox"]:checked').each(function(){
$('select#list'+i).val($(this).val());
i++;
});
});
this is the first html for checkbox
<input class="theme" type="checkbox" name="theme" value="Present" id="Present" />
<label for="Present">Present</label>
<input class="theme" type="checkbox" name="theme" value="Absent" id="Absent" />
<label for="Absent">Absent</label>
<input class="theme" type="checkbox" name="theme" value="Leave" id="Leave" />
<label for="Leave">Leave</label>
<input class="theme" type="checkbox" name="theme" value="Holiday" id="Holiday" />
<label for="Holiday">Holiday</label>
<br><br> <input type="button" class="Go" value="Go" />
这是另一个用于下拉列表的html
<select id='list1' name='att_status[]'>
<option value='Present'>Present</option>
<option value='Absent'>Absent</option>
<option value='Holiday'>Holiday</option>
<option value='Leave'>Leave</option>
</select>
嘿,你为什么这么做? window.onload=函数{} 此代码将函数设置为window.onload属性。 您必须这样定义该函数: $input.theme:checkbox.clickcountChecked; $'.Go'。单击函数{ var i=1; $'input[type=checkbox]:选中。每个函数{ $'selectlist'+i.val$this.val; i++; }; }; 函数计数已检查{ var n=$input.theme:checked.length; 如果n==1{ $'input.theme:checkbox:not:checked'.prop'disabled',true; }否则{ $'input.theme:复选框:未选中'.prop'disabled',false; } } 目前 缺席的 离开 假日 目前 缺席的 假日 离开 您可以尝试以下方法:
$(document).ready(function(){
$("input.theme:checkbox").click(countChecked);
$('.Go').click(function() {
var i=1;
$('input[type="checkbox"]:checked').each(function(){
$('select#list'+i).val($(this).val());
i++;
});
});
});
function countChecked() {
var n = $("input.theme:checked").length;
if (n == 1) {
$('input.theme:checkbox:not(:checked)').prop('disabled', true);
} else {
$('input.theme:checkbox:not(:checked)').prop('disabled', false);
}
}
为什么要使用window.onload?你调试了你的代码吗?或者只是这样:$input.theme:checkbox.onclick,函数{$'input.theme:checkbox:not:checked'.prop'disabled',$input.theme:checked.length==1};当我运行这段代码时,它说$notdefine。这就是我把窗户装上的原因。谢谢你回答我的问题。如果你阅读了这篇文章,它说函数赋值就像这个窗口一样。onload=countChecked{}会在加载事件触发后触发。这很好,但是你想使用这个函数,即使它没有在那个范围内定义。干杯!