Javascript 单选按钮、文本区和输入检查

Javascript 单选按钮、文本区和输入检查,javascript,jquery,Javascript,Jquery,我有一个表单,其中所有输入字段都是classitem。当我单击submit时,它会使用以下函数检查是否填写了所有值 $.each($(".items"),function(i,e){ // loop through all the items if(e.value == "" || !$("input[name='"+e.name+"']:radio:checked").length) if(error.indexOf(e.title) === -1)

我有一个表单,其中所有输入字段都是class
item
。当我单击submit时,它会使用以下函数检查是否填写了所有值

$.each($(".items"),function(i,e){
    // loop through all the items
    if(e.value == "" || !$("input[name='"+e.name+"']:radio:checked").length)
        if(error.indexOf(e.title) === -1)
            error += e.title + "<br/>";
    });
$。每个($(“.items”)、函数(即,e){
//循环浏览所有项目
if(e.value==“”| |!$(“输入[name=”+e.name+”]:收音机:选中”).length)
if(错误索引of(如标题)=-1)
错误+=e.title+“
”; });
此表单由文本区域、单选框和普通文本输入字段组成。它返回所有未填写的广播框,以及所有未填写的文本输入。但是它也会返回所有的文本区域,不管是否填充

我最初认为这是因为我指定它来检查值,但看起来值检查实际上检查文本区域,所以它不可能是这样

有人能帮我让它只返回空元素吗

$.each( $( '.items' ), function() {
    if ( ( this.type === 'checkbox' || this.type === 'radio' ) && !this.checked ) {
        // Go
    }
    else if ( this.value === '' ) {
        // Do your stuff
    }
});
不幸的是,似乎别无选择,只能将案件分开处理

$.each($('.items'),function(i,elem){
    if( ($(elem).attr('type') == 'radio' || $(elem).attr('type') == 'checkbox') && !$(elem).is(':checked') ) {
               // RADIOS AND CHECKBOXES EMPTY
    } else if( $(elem).val().length == 0 ) {
              // INPUTS TYPE TEXT AND TEXTAREA EMPTY
    }
});

不幸的是,似乎别无选择,只能将案例分开。

因此我结合使用了JBRTRND和Florian Margaine的答案,因为他们都不想100%正确地工作

$.each($('.items'),function(i,elem){
    if( ($(elem).attr('type') == 'radio' || $(elem).attr('type') == 'checkbox') && !$(elem).is(':checked') ) {
               // RADIOS AND CHECKBOXES EMPTY
    } else if( $(elem).val().length == 0 ) {
              // INPUTS TYPE TEXT AND TEXTAREA EMPTY
    }
});
我只是把这个放在这里,以防有人被困在同一个问题上。我从他们那里得到的任何帮助都不值得表扬,我真的很感谢他们

我就是这样修复的:

$.each($(".items"),function(){
    // loop through all the items
        // and alert the value
        if ( this.type == 'radio' && !$("input[name='"+this.name+"']:radio:checked").length) {
            if(error.indexOf(this.title) === -1)
                error += this.title + "<br/>";
        }
        else if ( this.value === '' ) {
            if(error.indexOf(this.title) === -1)
                error += this.title + "<br/>";
        }
    });
$。每个($(“.items”)、函数(){
//循环浏览所有项目
//并提醒该值
if(this.type=='radio'&&&!$(“输入[name='”+this.name+']:radio:checked”).length){
if(error.indexOf(this.title)=-1)
错误+=this.title+“
”; } else if(this.value==''){ if(error.indexOf(this.title)=-1) 错误+=this.title+“
”; } });
所以我结合使用了JBRTRND和Florian Margaine的答案,因为他们都不想100%正确地工作

我只是把这个放在这里,以防有人被困在同一个问题上。我从他们那里得到的任何帮助都不值得表扬,我真的很感谢他们

我就是这样修复的:

$.each($(".items"),function(){
    // loop through all the items
        // and alert the value
        if ( this.type == 'radio' && !$("input[name='"+this.name+"']:radio:checked").length) {
            if(error.indexOf(this.title) === -1)
                error += this.title + "<br/>";
        }
        else if ( this.value === '' ) {
            if(error.indexOf(this.title) === -1)
                error += this.title + "<br/>";
        }
    });
$。每个($(“.items”)、函数(){
//循环浏览所有项目
//并提醒该值
if(this.type=='radio'&&&!$(“输入[name='”+this.name+']:radio:checked”).length){
if(error.indexOf(this.title)=-1)
错误+=this.title+“
”; } else if(this.value==''){ if(error.indexOf(this.title)=-1) 错误+=this.title+“
”; } });
是的,这对我来说更清楚了。/另外,我猜对于文本区域,可能需要检查this.text???@zinking不需要检查文本区域,它不会通过
this.nodeName=='INPUT'
检查。如果文本区域为空,则不会通过任何文本区域。。。它还需要检查文本区域是否空白。哦,我误解了你的问题。它应该返回所有空白元素。目前我的问题是,它返回所有textareas,无论它们是否包含文本。我想帮你代表妓女时代,所以如果你能更新这个答案,我将投票并接受!:派普,这对我来说更清楚了另外,我猜对于文本区域,可能需要检查this.text???@zinking不需要检查文本区域,它不会通过
this.nodeName=='INPUT'
检查。如果文本区域为空,则不会通过任何文本区域。。。它还需要检查文本区域是否空白。哦,我误解了你的问题。它应该返回所有空白元素。目前我的问题是,它返回所有textareas,无论它们是否包含文本。我想帮你代表妓女时代,所以如果你能更新这个答案,我将投票并接受!:PStill返回所有文本区域,而不仅仅是空区域。谢谢!!我只需要将
$(elem).attr('type')='checkbox'
更改为
$(elem).attr('type')='radio
,并在if语句末尾添加另一个括号,它就起作用了。实际上,我错了,这不起作用。。。它仍然返回所有的广播框…仍然返回所有的文本区域,而不仅仅是空的。谢谢!!我只需要将
$(elem).attr('type')='checkbox'
更改为
$(elem).attr('type')='radio
,并在if语句末尾添加另一个括号,它就起作用了。实际上,我错了,这不起作用。。。它仍然返回所有的广播框…很高兴你把它整理好:)然后接受你的答案,它将使它进入顶部并帮助人们访问此页面。很高兴你把它整理好:)然后接受你的答案,它将使它进入顶部并帮助人们访问此页面。