我有一个包含多种类型输入字段的表单,其中包含一些复杂的验证,可以根据用户输入或选择的内容禁用/启用各种输入类型。我正在使用 JS 检查不同的启用输入类型,如果它们为空,则会保留运行总计。每次修改启用的输入时,CheckForm 函数都会运行。
但是,我遇到的问题是确定启用了哪些下拉选择类型,如果为空,则需要将它们计入 alt_count 变量中。如果您查看下面的代码,请注意复选框和普通文本输入类型工作正常,这只是单选类型的问题。
代码:
function CheckForm() {
var alt_count = 0;
var field_list = '';
$('tr#BaseTab td input:not(:button):not(:disabled), tr#BaseTab td select:not(:disabled)').each(function () {
if ($(this).attr('type') === 'checkbox') {
var temp_obj = $(this);
var temp_name = $(this).attr('name');
if ($('input[name=' + temp_name + ']:checked').length === 0) {
alt_count += 1;
}
} else if ($(this).attr('type') === 'select-one') {
if ($(this).attr("selectedIndex") === 0) {
alt_count += 1;
}
} else {
if ($(this).val() === '') {
alt_count += 1;
}
}
});
感谢您提供的任何帮助。
最佳答案
select
元素实际上不会具有属性 type="select-one"
。但是,DOM 元素将具有 .type
属性,允许您检查是否正在处理 select-one
还是 select-multiple
>。因此,不要检查“type”属性,而是像这样检查属性:
else if ($(this)[0].type === 'select-one') {
关于Javascript 识别启用的下拉选择类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24681952/