1

この投稿は私の前の質問に基づいています。3つのセクションがあるフォームがあり、ボタンを使用backnextて、一度に1つのテーブルセクションのみをユーザーに表示および非表示にします。さらに、nextボタンは検証トリガーとしても機能します。

私の質問は:

  1. 私の検証アプローチでは、ドロップダウンフィールドをチェックできませんでした。
  2. jQuery validation plugin類似した名前のフィールドを検証できるという明確な方法はありますか?たとえば、、、、およびという名前のフィールドがありmm1ます。検証ルールなどのショートカット方法を使用できますか?mm2mm3mm*
  3. number組み込みの方法では科学的な入力を検証できないことがわかりました。それを処理する他の組み込み方法はありますか?このために独自のメソッドを追加する必要がありますか?

入力ありがとうございます!これがデモです。

HTML

<form method="post" id="form1" action=index.html>
    <table>
         <H4 align="center" id="id_tab">
            |<a href="#" class="Chemical"> Chemical </a>|
             <a href="#" class="Application"> Application </a>|
             <a href="#" class="Physical"> Physical </a>|
            </H4>
    </table><br>
    <table class="tab tab_Chemical" border="0">
        <tr><th><label for="id_wat_hl">Water Column Half life (days):</label></th>
            <td><input type="text" name="wat_hl" id="id_wat_hl" value="1e-08" /></td></tr>
    </table>
    <table class="tab tab_Application" border="0" style="display:none">
        <tr><th scope="col"><label for="id_noa">Number of Applications:</label></th>
             <td scope="col"><select name="noa" id="id_noa">
                 <option value="">Make a selection</option>
                 <option value="1">1</option>
                 <option value="2">2</option>
                 <option value="3">3</option></select>
             </td>
         </tr>    
    </table>    
    <table class="tab tab_Physical" border="0" style="display:none">
        <tr><th><label for="id_mas_tras_cof">Mass Transfer Coefficient (m/s):</label></th>
            <td><input type="text" name="mas_tras_cof" id="id_mas_tras_cof" /></td></tr>
    </table>
    <table align="center">
        <tr><td><input class="back" type="button" value="Back" /></td>
            <td><input class="next" type="button" value="Next" /></td>
            <td><input class="submit" type="submit" value="Submit" /></td></tr>
    </table></form>

JS

$(document).ready(function () {
    var tab_pool = ["tab_Chemical", "tab_Application", "tab_Physical"];
    var visible = $(".tab:visible").attr('class').split(" ")[1];
    var curr_ind = $.inArray(visible, tab_pool);
    $(".submit").hide();
    $(".back").hide();

    var validator = $('form').validate({
        ignore: 'input[type="button"],input[type="submit"]',
        rules: {
            wat_hl: {
                required: true,
                number: true
            },
            noa: {
                required: true
            },
            mm1: {
                required: true
            },            
            mm2: {
                required: true
            },              
            mm3: {
                required: true
            },              
            dd1: {
                required: true
            },            
            dd2: {
                required: true
            },              
            dd3: {
                required: true
            },               
            mas_tras_cof: {
                required: true
            }
        }
    });


    $('.next').click(function () {
        var tab = $(".tab:visible");

        var valid = true;
        $('input', tab).each(function (i, v) {
            valid = validator.element(v) && valid;
        });

        if (!valid) {
            return;
        }

        if (curr_ind < 2) {
            $(".tab:visible").hide();
            curr_ind = curr_ind + 1;
            $("." + tab_pool[curr_ind]).show();
            $(".submit").hide();
            $(".back").show();
        }
        if (curr_ind == 2) {
            $(".submit").show();
            $(".next").hide();
        }
    });

    $('.back').click(function () {
        if (curr_ind > 0) {
            $(".tab:visible").hide();
            curr_ind = curr_ind - 1;
            $("." + tab_pool[curr_ind]).show();
            $(".submit").hide();
            $(".next").show();
        }
        if (curr_ind == 0) {
            $(".back").hide();
        }
    });

    var i = 1
    $('.tab_Application').append('<tr id="noa_header" style="display:none"><th width="18%">App#</th><th width="18%">Month</th><th width="18%">Day</th>');

    $('#id_noa').change(function () {
        var total = $(this).val()
        $('tr[id*="noa_header"]').show()

        while (i <= total) {
            $('.tab_Application').append('<tr class="tab_noa1"><td><input type="text" size="5" value="' + i + '"  disabled/></td><td><input type="text" size="5" name="mm' + i + '" id="id_mm' + i + '""/></td><td><input type="text" size="5" name="dd' + i + '" id="id_dd' + i + '""/></td>');
            i = i + 1;
        }
        while (i - 1 > total) {
            $(".tab_Application tr:last").remove();
            i = i - 1
        }
        $('</table>').appendTo('.tab_Application');
    })
});  
4

2 に答える 2

2

質問 1: コードには以下が含まれます。

$('input', tab).each(function (i, v) {
        valid = validator.element(v) && valid;
    });`

しかし、 aselectは ではありませんinput。に変更し$('input,select', tab)ます。

質問 2:クラスごとにルールを適用する方法については、この質問を参照してください。また、入力に検証メソッドの名前であるクラスがある場合、メソッドが適用されるためclass="required number"、入力で使用できます。また、プラグインは HTML5 検証タグも認識<input type="number" required/>するため、適切な検証を行うことができます。

質問 3: 正規表現を使用できます。この質問を参照してください。

于 2013-03-01T03:28:05.077 に答える
2

引用OP:

2) 同様の名前のフィールドを検証できる jQuery 検証プラグインの明示的な方法はありますか? たとえば、 name mm1mm2、および のフィールドがありmm3ます。mm*検証ルールなどのショートカットの方法を使用できますか?

はい。

$('[name*="mm"]').each(function() {
    $(this).rules('add', {
        required: true,
        digits: true,
        messages: {
            required: "custom message required",
            digits: "custom message digits"
        }
    });
});

ドキュメントを参照してください: http://docs.jquery.com/Plugins/Validation/rules#.22add.22rules

デモ: http://jsfiddle.net/g7XES/


OPのコメントへの対応:

additional-methods.jsファイル内には、 というルールがありますinteger

jQuery.validator.addMethod("integer", function(value, element) {
    return this.optional(element) || /^-?\d+$/.test(value);
}, "A positive or negative non-decimal number please");

デモ: http://jsfiddle.net/v82sZ/

于 2013-03-01T03:43:32.883 に答える