如何检查在JavaScript和PHP中是否设置了jQuery属性



在我开始之前,让我澄清一下我是jQuery和JavaScript的新手。如果答案很明显,我很抱歉😅好吧,我试图创建一个页面,用户可以选择日期和时间,并希望使用JavaScript和PHP来确保用户不返回一个空白字段(如没有日期或时间设置和点击提交)。我正在使用jQuery的日期选择器和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>

代码未经过测试,但应该足以让您开始。

相关内容

  • 没有找到相关文章

最新更新