簡體   English   中英

僅當在步驟向導表單中填寫所有字段時,才激活提交按鈕

[英]activate submit button only when all the fields are filled in step wizard form

我有一個水平的一頁網站,每頁中包含多步表單,並在其后單擊下一步按鈕以重定向到第二頁。

我希望用戶在填寫給定表單中的所有必填字段之前不能進入下一頁。

這是節略的代碼,在月份,年份和國籍方面只有有限的選擇:

    <div class="step-content hide">
    <div class="row">
        <div class="col-sm-12">
            <div class="col-sm-4" id="artle_tellusmore_label_para" style="float:left;">
                <label id="artle_tellusmore_label">Date of Birth</label id="artle_tellusmore_label">
                <label id="artle_tellusmore_label">Gender</label>

                <hr>
                <div style="margin-left: 38px;" id="artle_tellusmore_profileimageupload">
                    <img style="height: 180px; width:165px;" id="output" src="image/artle_dp_dummy.png">

                </div>
            </div>
            <div style="margin-left: 248px;" class="col-sm-8">
                <div id="artle_date_of_birth_combo_box">
                    <select style="width: 60px;" name="birthday">
                        <option value="">-Day-</option>
                        <option value="">1</option>
                        <option value="">2</option>

                    </select>

                    <select style="width: 91px;" name="birthmonth">
                        <option value="">-Month-</option>
                        <option value="">January</option>
                        <option value="">February</option> 
                    </select>

                    <select style="width: 67px;" name="birthyear">
                        <option value="">-Year-</option>
                        <option value="2007">2007</option>
                        <option value="2006">2006</option>

                    </select>
                </div>
                <div id="artle_tellusmore_gender_radio_button">
                    <input class="radio" type="radio"><span id="">Male</span>&nbsp&nbsp&nbsp&nbsp&nbsp

                    <input class="radio" type="radio"><span id="">Female</span>
                </div>

                <div id="artle_tellusmore_phone_number">

                    <input style="width: 223px; margin-top: 10px; height: 33px;" required="" pattern="[0-9]{10}" x-moz-errormessage="Enter a valid phone number" size="10" value="" placeholder="Phone Number" type="tel">
                </div>

                <div id="artle_tellusmore_nationality">
                    <select name="nationality" style="width: 225px;">
                        <option value="">-- Nationality --</option>
                        <option value="afghan">Afghan</option>
                        <option value="albanian">Albanian</option>

                        <option value="zimbabwean">Zimbabwean</option>
                    </select>
                </div>
                <div id="artle_tellusmore_state">
                    <input type="text" value="" placeholder="State" style="width: 223px; height: 33px;" />

                </div>
                <hr>
                <div id="artle_tellusmore_">
                    <input type="file" name="file" accept="image/*" onchange="loadFile(event)" id="artistprofileuploadfileField">

                    <p style="margin-top: 15px; font-size: 1.1em;">You can upload a jpg, gif or png files</p>
                </div>

            </div>

        </div>
        <div style="clear:both;"></div>
        <button class="next-button btn hide" id="artle_next_button">Next</button>
        <input type="submit" class="submit-button btn" id="artle_next_button" value="Done" />
        <button class="back-button btn" id="artle_next_button">Back</button>
    </div>

</div>

通過查看您的html,我認為您沒有使用HTML5。 我假設您希望對所有帶有類form-field的表單域進行驗證。

在這種情況下,您可以使用以下代碼:

首先檢查所有表單元素是否具有值:

function validateForm(form) {
  var isValid = true;
  $('.form-field').each(function() {
    if ( $(this).val() === '' )
        isValid = false;
  });
  return isValid;
}

然后使用此更改事件來跟蹤表單元素是否有任何更改,然后驗證表單並啟用或禁用下一個按鈕

 $(".form-field").change(function() {
      if (validateForm() )
        $(".next-button").show() 
      else 
        $(".next-button").hide() 
    });

請以上述代碼為指導。

由於您嘗試過的代碼不足,無法給出確切答案。 但您正在尋找這樣的東西。

$(document).ready(function(){
    $('form').find('select').change(function() {
        var disabled = false;
        $('form').find('select').each(function() {
            if ($(this).find('option:selected').val() === '') {
                disabled = true;
            }
        });
        $('form').find('input[type="submit"]').prop('disabled', disabled);
    });
});

為了驗證所有輸入,您可以執行以下操作。

$("form").submit(function(event) {
    var isValid = true;
    $('input[type="text"]').each(function() {
        if ( $(this).val() === '' )
            isValid = false;            
    });
    if (!isValid) event.preventDefault();
});

用於針對所有輸入激活提交按鈕。

$('input[type="text"]').on('input', function() {
    var isValid = true;
    $('input[type="text"]').each(function() {
        if ( $(this).val() === '' )
            isValid = false;            
    });
    if (!isValid) $('input[type="submit"]').attr('disabled','disabled');
    else $('input[type="submit"]').removeAttr('disabled');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM