【问题标题】:How to check if jQuery properties have been set in JavaScript and PHP?如何检查是否在 JavaScript 和 PHP 中设置了 jQuery 属性?
【发布时间】:2016-10-26 05:17:58
【问题描述】:

在此之前,让我澄清一下我是 jQuery 和 JavaScript 的新手。如果答案很明显,我很抱歉???? 好的,我正在尝试创建一个页面,用户可以在其中选择日期和时间,并希望使用 JavaScript 和 PHP 来确保用户没有返回空白字段(例如没有设置日期或时间并单击提交)。我正在使用 jQuery 的 datepicker 和 Jon Thornton 的 timepicker。我成功创建了显示该字段的字段,但我不知道在 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>

当我点击按钮提交时,我没有收到日期未设置的警告。如果您能告诉我一个方法,将不胜感激。 提前谢谢!

【问题讨论】:

  • console.log(date) in validateForm 以查看未选择任何内容时获得的值
  • 它说“create_dates.inc.php:44 Uncaught ReferenceError: Invalid left-hand side in assignment”
  • 好吧,这看起来不像您发布的任何代码,所以还有其他问题......以及
  • 哦,等等。由于看起来多余,我又删除了几列。我现在正在更新它。我真的很抱歉。
  • 仍然没有无效的左侧 - 您的浏览器肯定会显示错误的行

标签: javascript php jquery jquery-ui-datepicker jquery-ui-timepicker


【解决方案1】:

为了更进一步,为什么不实现 jQueryValidate? (https://jqueryvalidation.org/)

这对于你需要的东西来说有点矫枉过正当你正在实现其他验证功能时,它值得添加,但只需在输入元素中添加“必需”,它就会满足你的要求 -当您开始开发更多功能时,您会非常喜欢可用的附加功能。

<!-- 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>

代码未经测试,但足以让您入门。

【讨论】:

  • 非常感谢你的技巧。但我的要求之一是专门使用 JavaScript 来验证这些值。仍然非常感谢你:)
  • 这是 Javascript。但是,如果您需要做一些非常具体的事情,jQueryValidate 非常灵活,您可以为那些(罕见的)内置函数不起作用的情况添加任何特定于 javascript 的验证。它只是更好地处理捕获提交、显示错误以及您将要编写的大量代码。默认情况下它会显示错误,但如果需要,您可以捕获并更改为alerts()。无论如何,你的选择,但我鼓励你去看看。
  • 非常感谢!现在我有更多的技巧可以向我的朋友们吹嘘(我们才刚刚开始):)
猜你喜欢
  • 2015-10-27
  • 2011-11-03
  • 1970-01-01
  • 2012-05-11
  • 2013-10-01
  • 2016-09-03
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多