简体   繁体   English

如何为此写一个条件语句?

[英]How to write a conditional statement for this?

I need your help in writing an if statement in JS. 在JS中编写if语句时,我需要您的帮助。 I'm still a newbie and I have no idea on how to start with the if statement. 我仍然是新手,我不知道如何从if语句开始。 I have this code that launches a form that is inside a modal. 我有这段代码可以启动模态内部的表单。 What I wanted to do is that when the form is complete, it would launch another modal, confirming the entries that has been inputted in the form, and if the form is not complete, validations would appear, and it would not launch the confirmation modal. 我想做的是,当表单完成时,它将启动另一个模式,确认已在表单中输入的条目,如果表单不完整,将出现验证,并且它将不启动确认模式。 I used jquery.validate for the validations of the form. 我使用jquery.validate进行表单验证。

Here is my code: 这是我的代码:

Form inside the modal: 模态内的形式:

<form id="formnew" class="row form-columned" role="form">

  <div class="col-md-6 m-b-10 p-t-15">
    <label for="vname">Vehicle Name:</label>
    <input type="text" autocomplete="off" class="input-focused m-b-10 m-t-10 input-sm form-control" id="vname" name="vname" placeholder="Vehicle Name">
  </div>

  <div class="col-md-6 m-b-10 p-t-15">
    <label>Category:</label>
    <select name="category" autocomplete="off" class="input-focused m-b-10 m-t-10 input-sm form-control mask-date">
      <option value="category">Category</option>
      <option>Car</option>
      <option>Bus</option>
      <option>Truck</option>
      <option>Van</option>
      <option>Motorcycle</option>
    </select>
   </div>

   <div class="pull-right m-t-20">
     <button href="#ValidateForm" data-toggle="modal" data-dismiss="modal" type="submit" class="btn btn-lg">Create</button>
     <button data-dismiss="modal" class="btn btn-lg">Cancel</button>
   </div>
</form>

Confirmation Modal: 确认方式:

<form class="row form-columned" role="form">
  <div class="col-md-12">
    <p>Confirm changes?</p>
  </div>

  <div class="pull-right m-t-20 m-r-5">
    <button type="submit" data-dismiss="modal" class="btn btn-lg">Okay</button>
    <button type="submit" data-dismiss="modal" class="btn btn-lg">Cancel</button>
  </div>
</form>

JS for Confirmation Modal: 确认模式的JS:

function confirm() {

$("#CreateModal").click(function () {

   $('#ValidateForm').modal("show");
 });
}

Validation Rules: 验证规则:

$(document).ready(function () {
$("#formnew").validate({

    rules: {
        vname: "required",
        category: { valueNotEquals: "category" }
    },

    messages: {
        vname: { required: "Please provide the Vehicle Name" },
        category: { valueNotEquals: "Please select a Category" }
    },


});
});

I would suggest not to use another modal inside a model as it might complicate things. 我建议不要在模型中使用其他模态,因为这可能会使事情复杂化。 I ran into same situation and here what I did: 我遇到了同样的情况,在这里我做了什么:

Make two section in your form: 在表单中分成两部分:

<form>
   <div class='data'>
     <!-- data input and confirm button-->
   </div>
   <div class='confirm'>
   <!-- confirm message and submit button-->
   </div>
</form>

Initially confirm div will be hidden and when user click on confirm button do following: 最初,确认div将被隐藏,当用户单击确认按钮时,请执行以下操作:

  1. Validate user input 验证用户输入
  2. if error, show message 如果错误,显示消息
  3. else hide data div and show confirm div 否则隐藏数据div并显示确认div

There user can now submit form by clicking submit button. 用户现在可以通过单击提交按钮来提交表单。 Hope it helps.. :-) 希望能帮助到你.. :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM