Jquery Ajax验证复选框 [英] Jquery Ajax Validate Checkboxes

查看:58
本文介绍了Jquery Ajax验证复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个简单的解决方案,所以我有点尴尬,但JS并不是我的强项所以我想我会问。

Probably a simple solution so I'm a bit embarassed, but JS is not really my forte so I figure I'll ask.

我正在使用 Jquery Form 插件,为我正在制作的活动策划应用程序提交一组复选框。

I'm using the Jquery Form plugin to submit a group of checkboxes for requirements for an event planning app I am making.

如果没有要求的数组键,我无法进行验证预先提交回调拒绝表单。我知道在php中我可以简单地使用像array_key_exists这样的东西或只是检查isset(),但我不确定js中的同源词是什么。代码如下。

I'm having trouble making my validation presubmit callback refuse the form if there is no array key for 'requirement'. I know in php I could simply use something like array_key_exists or just check against isset(), but I'm not sure what the cognate is in js. code follows.

<form id="choose_reqs" method="post" action="http://www.domain.com/generator/chooseReqs/" enctype="multipart/form-data">
            <p>I'm planning on getting:</p>
            <?php foreach($_SESSION['event']->opt_r as $r){?>
                <span style="display:block; width:120px; padding:4px; border:1px #ccc solid;"><input type="checkbox" value="<?=$r;?>" name="requirement[]"/><?=$r;?></span>
            <?php }?>
            <input type="submit" name="event_chosen" value="Next" />
        </form>

然后在加载表单后运行的关联js:

And then the associated js that runs after the form is loaded in:

 function eventTypeChosen(responseText, statusText, xhr, $form)  {

   var options = {  
        target:        '#app',  
        beforeSubmit: formSubmitCheck,
        success:       reqsChosen  
    }; 
        setNav();
        $('#choose_reqs').ajaxForm(options); 
   } 

   function setNav(){
        $('#start_over').click(start);
   }


   function formSubmitCheck(formData, jqForm, options){

        if(formData.hasOwnProperty('requirement')){ 
            alert('Please check at least one requirement'); 
            return false; 
        }else{

    $(jqForm).fadeOut(200);
    return true;
    }
   }

.hasOwnProperty()方法显然有问题以及我如何使用它。

Obviously something is wrong with the .hasOwnProperty() method and how I'm using it.

推荐答案

function formSubmitCheck(formData, jqForm, options){

        if($('input[name=requirement[]]').fieldValue().length==0){ 
            alert('Please check at least one requirement'); 
            return false; 
        }else{

    $(jqForm).fadeOut(200);
    return true;
    }
   }

这篇关于Jquery Ajax验证复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆