开发者

Jquery Ajax Validate Checkboxes

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

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

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>

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,
  开发者_StackOverflow中文版      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;
    }
   }

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;
    }
   }


From the form plugin page you linked in your comments:

formData is an array of objects representing the name and value of each field that will be sent to the server...

Try printing out the formData object to the console so you can verify its structure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜