【问题标题】:Open modal upon submit button after validation验证后在提交按钮上打开模式
【发布时间】:2018-11-05 06:27:35
【问题描述】:

如果用户输入了一些值,我想在点击提交按钮时打开一个模态,否则不需要打开模态。

目前,当单击 sumbit 按钮时,模式正在打开而未验证 必需 字段

这是表格

<form action=" " method="POST">
    <label> Employee ID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</label>
    <input type="text" name="id" placeholder="Enter Employee ID" required><br><br><br>
    <input type="submit"  onclick="mdl1()" value="Update">
    <button type="submit"  onclick="mdl2()" style="margin-left:100px;"> Delete</button><br>
</form>

更新模式

<div id="Modal_update" class="modal">
    <div class="modal-content">
    <span class="close" onclick="close_mdl1()">&times;</span>
    <div class="container">
      <form action="home.php" method="POST">
        <label><b>Employee ID</b></label>
        <input type="text" placeholder="Enter User ID" name="id" value="<?php echo $_SESSION['ID'] ?>" required>
        <label><b>Name</b></label>
        <input type="text" placeholder="Enter Name" name="name" value="<?php echo $_SESSION['Name'] ?>" required>
        <label><b>Employee Type</b></label>
        <input type="text" placeholder="Temporary/Permanent" name="emp_type" value="<?php echo $_SESSION['Emp_type'] ?>" required>
        <button type="submit" class="modalbutton" name="update">Update</button><br>
      </form>
    </div>
  </div>
</div>

mdl1() 的代码

function mdl1() {
  document.getElementById('Modal_update').style.display = "block";
}

function close_mdl1() {
  document.getElementById('Modal_update').style.display = "none";
}

window.onclick = function(event) {
  if (event.target == document.getElementById('Modal_update')) {
    document.getElementById('Modal_update').style.display = "none";
  }
}

提前致谢!

【问题讨论】:

  • 请将按钮类型“提交”更改为“按钮”。然后检查它。
  • 我没有看到你在哪里给你的任何输入一个 id。
    @Difster 我已将 id 分配给 Div 而不是输入

标签: javascript php html


【解决方案1】:

试试这个:

function mdl1() {
  var id = document.getElementsByName("id")[0].value;
  if(id != ''){
    document.getElementById('Modal_update').style.display = "block";
  }else{
    alert('Employee ID can not be empty.');
  }
}

注意:请确保您的姓名不同。

【讨论】:

  • 但是我怎样才能对这个输入使用一些 php 验证呢?
  • $id = $_POST['id']; if($id == ''){ echo '员工ID不能为空。'; }else{ //保存到数据库或你想要的 }
【解决方案2】:

为表单标签提供id 属性,例如-&lt;form action=" " method="POST" id="form_id"&gt;,并且在调用模态时可以使用-

if($("#form-id").valid())
{
   //call your modal
   //if you are using latest version of js, you could this syntax
   $("#Modal_update").modal('show');   
}    

确保在代码中添加jquery.validate.js

【讨论】:

  • 我会尝试使用此代码,但我在 1 个表单中有 2 个提交按钮
  • 不要使用提交按钮,两个按钮都使用&lt;button type="button"&gt;标签,并使用ajax提交表单。
  • 我不熟悉ajax
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
相关资源
最近更新 更多