我有一个包含多种类型输入字段的表单,其中包含一些复杂的验证,可根据用户输入或选择的内容禁用/启用各种输入类型。我使用JS来检查不同的启用输入类型,如果它们是空的,它会保持运行总计。每次修改启用的输入时,CheckForm函数都会运行。
但是,我遇到的问题是确定哪些下拉选择类型已启用,如果为空,则需要在alt_count变量中计算。如果您查看下面的代码,请注意复选框和普通文本条目类型工作正常,这只是select-one类型的问题。
代码:
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;
}
}
});
感谢您提供的任何帮助。
答案 0 :(得分:0)
select
元素实际上不具有属性type="select-one"
。但是,DOM元素将具有.type
属性,允许您检查是否正在处理select-one
或select-multiple
。因此,不要检查'type'属性,而是检查属性,如下所示:
else if ($(this)[0].type === 'select-one') {