簡體   English   中英

如何檢查是否在JavaScript和PHP中設置了jQuery屬性?

[英]How to check if jQuery properties have been set in JavaScript and PHP?

在此之前,讓我澄清一下我是jQuery和JavaScript的新手。 抱歉,如果答案很明顯😅好的,我正在嘗試創建一個頁面,用戶可以在其中選擇日期和時間,並希望使用JavaScript和PHP確保用戶未返回空白字段(例如沒有日期)或設置的時間,然后點擊“提交”。 我正在使用jQuery的datepicker和Jon Thornton的時間選擇器。 我成功創建了顯示這些字段的字段,但是我不知道在JavaScript中進行檢查和檢查的正確方法。 到目前為止,這是我的代碼:

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script type='text/javascript' src='../jQuery/jonthornton-jquery-timepicker-dbdea8e/jquery.timepicker.js'></script>
<link rel='stylesheet' href='../jQuery/jonthornton-jquery-timepicker-dbdea8e/jquery.timepicker.css'>
<script>
  $( function() {
    $( "#datepicker" ).datepicker({maxDate: "+2M", dateFormat: 'yy-mm-dd'});
  } );
    $(function(){
   $('.scrollDefaultExample').timepicker({ 'scrollDefault': 'now' }); 
});
</script>

<script>
function validateForm() {
    var date = document.forms["Time_Slot"]["datePick"].value;
    var date2 = document.getElementById("datepicker");
    var main_timeSlot = document.forms["Time_Slot"]["timeSlot"].value;
    var add1 = document.forms["Time_Slot"]["additional_1"].value;
    var add2 = document.forms["Time_Slot"]["additional_2"].value;
    var add3 = document.forms["Time_Slot"]["additional_3"].value;
    var add4 = document.forms["Time_Slot"]["additional_4"].value;
    var add5 = document.forms["Time_Slot"]["additional_5"].value;
    var add1Box = document.forms["Time_Slot"]["additional_1_box"].value;
    var add2Box = document.forms["Time_Slot"]["additional_2_box"].value;
    var add3Box = document.forms["Time_Slot"]["additional_3_box"].value;
    var add4Box = document.forms["Time_Slot"]["additional_4_box"].value;
    var add5Box = document.forms["Time_Slot"]["additional_5_box"].value;
    alert("yolo");
    console.log(date);
    if (date2 == null || date == "" || date == " ") {
        alert("A date must be chosen!");
        return false;
    }
    else if (lname == null || lname == "" || lname == " ") {
        alert("The last name field should be complete!");
        return false;
    }
    else if (email == null || email == "" || email == " ") {
        alert("The email field should be complete!");
        return false;
    }
    else if (password == null || password == "" || password = " ") {
        alert("A valid password must be entered! Note: it cannot contain just a space.");
        return false;
    }
}
</script>

<form name="Time_Slot" method="POST">
    <fieldset>
        <p>Date: <input type="text" id="datepicker" name="datePick"></p>
        <p>Time: <input type="text" class="scrollDefaultExample" name="timeSlot"></p>
        <br>
        <label><input type="checkbox" name="additional_1" value="extra_1"> Additional Time Slot: <input type="text" class="scrollDefaultExample" name="additional_1_box"></label> <br><br>

        <label><input type="checkbox" name="additional_2" value="extra_2"> Additional Time Slot: <input type="text" class="scrollDefaultExample" name="additional_2_box"></label> <br><br>

        <label><input type="checkbox" name="additional_3" value="extra_3"> Additional Time Slot: <input type="text" class="scrollDefaultExample" name="additional_3_box"></label> <br><br>

        <label><input type="checkbox" name="additional_4" value="extra_4"> Additional Time Slot: <input type="text" class="scrollDefaultExample" name="additional_4_box"></label> <br><br>

        <label><input type="checkbox" name="additional_5" value="extra_5"> Additional Time Slot: <input type="text" class="scrollDefaultExample" name="additional_5_box"></label> <br><br>

        <p><input type="submit" name="submit" value="Create Available Appointments"  onclick="return validateForm()"></p>
    </fieldset>
</form>

當我單擊按鈕提交時,不會警告我未設置日期。 如果您能告訴我一種方法,將不勝感激。 先感謝您!

更進一步,為什么不實現jQueryValidate? https://jqueryvalidation.org/

在實現其他驗證功能時,這 有點多余了,您 值得添加,但是只需在輸入元素中添加“ required”,它就可以滿足您的要求-當您開始開發更多功能時,您將真正例如可用的其他功能。

<!-- include the validation script, from CDN or download -->
<script src='    http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js'></script>

<!-- On document ready, find hte form and add the validation function -->
<script>
   $.ready(function() {
         $('form[name="Time_Slot"]').validate();
   });
</script> 

<!-- In the input, add "required" -->
<input type="text" id="datepicker" name="datePick" required>

該代碼未經測試,但是足以讓您入門。

暫無
暫無

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

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