2

因此,我正在尝试验证一个包含 4 个部分并分为不同页面的 JQM 表单,因此在验证第一部分之后,如果表单有效,我想转到第二部分。我正在尝试使用 JQuery.validate 插件进行验证,但它不会以任何方式响应表单。

PS:它必须与 JQuery.validation 插件一起使用

 <div data-role="page" id="page2" data-add-back-btn="true">
        <div data-role="header" data-position="fixed">
                <h1 id="page2Header"></h1>

        </div>
        <div data-role="content">

    <form id="sampleProperties">
                    <label for="station">Select Station:</label>
                    <select  name="station" id="station" onBlur="storeData(this.id,this.value)" required="true">
                        <option value="50028000">Tanama River</option>
                        <option value="50010500">Rio Guajataca, Lares</option>
                        <option value="60008002">Example River2</option>
                        <option value="60008003">Example River3</option>
                        <option value="60008004">Example River4</option>
                    </select>
                    <div class="hide">
                    <div class='suspendedAdditionalFields'>
                    <label for="sampleMediumDropdown">Select sample medium:</label>
                    <select name="sampleMediumDropdown" id="sampleMediumDropdown" onBlur="storeData(this.name,this.value)">
                        <option value="WS">WS(Surface Water)</option>
                        <option value="WSQ">WSQ(Surface Water QC)</option>
                    </select>
                    </div>
                    <div class='bottomAdditionalFields'>
                    <label for="sampleMediumDropdownBottom">Select sample medium</label>
                    <select name="sampleMediumDropdown" id="sampleMediumDropdownBottom" onBlur="storeData(this.name,this.value)">
                        <option value="SB">SB (bottom material)</option>
                        <option value="SBQ">SBQ (bottom material QC)</option>
                    </select>
                    </div>
                    </div>
                    <label for="date">Begin Date:</label>
                    <input name="date" id="beginDate" type="date" onChange="storeData(this.id,this.value)" />

                    <div class="hide">
                    <label for='EVENT'>Hydrologic event:</label>
                    <select name="EVENT" id="EVENT" onBlur="storeData(this.name,this.value)">
                        <option value="1">1- Drought</option>
                        <option value="2">2- Spill</option>
                        <option value="3">3- Regulated Flow</option>
                        <option value="4">4- Snowmelt</option>
                        <option value="5">5- Earthquake</option>
                        <option value="6">6- Hurricane</option>
                        <option value="7">7- Flood</option>
                        <option value="8">8- Volcanic activity</option>
                        <option value="9" selected>9- Routine Sample</option>
                        <option value="A">A- Spring breakup</option>
                        <option value="B">B- Under ice cover</option>
                        <option value="C">C- Glacial lake outbreak</option>
                        <option value="D">D- Mudflow</option>
                        <option value="E">E- Tidal action</option>
                        <option value="F">F- Fire, affected by fire prior sampling</option>
                        <option value="H">H- Dambreak</option>
                        <option value="J">J- Storm</option>
                        <option value="K">K- Backwater</option>
                        <option value="X">X- Not applicable</option>
                    </select>
                    </div>
                    <div class="ui-grid-a">
                     <label for="containerCuantity">Add a group of single container or sets of multiple samples: </label>    
                     <div class="ui-block-a">
                        <input type="number" min="1" value="1" max="40" id="containerCuantity" name="containerCuantity" onBlur="storeData(this.id,this.value)"/></div>
                     <div class="ui-block-b">
                        <select id="singleMultiContainer" name="singleMultiContainer"  onChange="storeData(this.id,this.value)">
                            <option value="" selected>--Select Container or Set--</option>
                            <option value="single">Single container sample</option>
                            <option value="multi">Multiple sets container</option>
                        </select>
                        </div>
                    </div>
                 <input id='addSampleParameters'type="submit"  value="Next"/>
                </form>
</div>
    <div data-role="footer" data-position="fixed">
        <div data-inline="true" data-type="horizontal"><a href="#" data-icon="bars" data-iconpos="notext" data-inline="true" data-mini="false" data-role="button">Menu</a><a id="addSampleParameters" data-icon="arrow-r" data-iconpos="right" data-inline="true" data-role="button" data-mini="false" data-theme="e">Next</a>
        </div>
    </div>
</div>

js:

$('#page2').bind('pageinit',function(){
  $('#sampleProperties').validate({
    rules:{
        station: 'required',
        date: 'required',
        singleMultiContainer: 'required',
        containerCuantity:{
            required:'true',
            minlength:1,
            maxlength:40
        }
    },
     submitHandler: function(form) {
                    alert('Success!');
                    }

  });
 });
4

2 回答 2

4

阶梯形式有多种方法。

当我创建多步骤表单时,我<form>为每个部分使用一组唯一的标签。然后我使用.valid()方法测试该部分,然后再转到下一个。 (不要忘记首先初始化插件;.validate()在 DOM 上的所有表单上调用 , 准备就绪。)

然后在最后一部分,我.serialize()在每个表单上使用并将它们连接成要提交的数据查询字符串。

像这样的东西...

$(document).on('pageinit', function() { // dom ready handler for jQuery Mobile

    $('#form1').validate({ // initialize form 1
        // rules
    });

    $('#gotoStep2').on('click', function() { // go to step 2
        if ($('#form1').valid()) {
            // code to reveal step 2 and hide step 1
        }
    });

    $('#form2').validate({ // initialize form 2
        // rules
    });

    $('#gotoStep3').on('click', function() { // go to step 3
        if ($('#form2').valid()) {
            // code to reveal step 3 and hide step 2
        }
    });

    $('#form3').validate({ initialize form 3
        // rules,
        submitHandler: function (form) {
           // serialize and join data for all forms
           var data = $('#form1').serialize() + '&' + $('#form2').serialize() + '&' + $(form).serialize()
           // ajax submit
           return false; // block regular form submit action
        }
    });

    // there is no third click handler since the plugin takes care of this 
    // with the built-in submitHandler callback function on the last form.

});

重要的是要记住我click上面的处理程序没有使用type="submit"按钮。这些是常规按钮,位于标签之外或.formtype="button"

只有最后一个表单上的按钮是常规type="submit"按钮。那是因为我只在最后一个表单上利用了插件的内置submitHandler回调函数。

“概念证明”DEMO:http: //jsfiddle.net/8bnZV/

顺便说一句,请注意我是如何.bind()用更现代的.on()方法替换你过时的。

另外,请参阅以供参考:

https://stackoverflow.com/a/17975061/594235

于 2013-10-23T16:00:53.897 回答
-2

编辑:显然不是一个有效的答案,因为页面在单独的 div 中;)

你有什么理由这样使用 .bind 吗?

这部分验证代码看起来不错:

$('#sampleProperties').validate({
  rules:{
    station: 'required',
    date: 'required',
    singleMultiContainer: 'required',
    containerCuantity:{
        required:'true',
        minlength:1,
        maxlength:40
    }
  },
  submitHandler: function(form) {
     alert('Success!');
  }
});

相反(您的标题不明确)如果您想要一种方法来验证没有 jquery.validate 的表单,您可以简单地将一些验证函数绑定到keyUp各种输入框上的事件。

于 2013-10-23T14:42:13.417 回答