【问题标题】:Can't Pass Values to Javascript Modal (Not Bootstrap)无法将值传递给 Javascript 模态(非引导程序)
【发布时间】:2018-08-22 00:17:43
【问题描述】:

我在尝试将值传递给非引导模式时遇到问题。 我当前的 php 循环的编写方式我了解我只能使用我的模式来编辑我的 SQL 表中的最后一行。但是,当我的模态 div 在循环内时,我只能更改 SQL 表的第一行。如何修改我的脚本以接受来自加载我的模式的按钮的值?

PHP:

<?php
while ($row=mysqli_fetch_array($select)) 
{
$idtemp=$row['id'];
<button id="edit_form<?php echo $idtemp;?>"  data-target="#id02" data-id="<?php echo $idtemp;?>"  onclick="document.getElementById('id02').style.display='block'">Edit</button>
 <?php
}
?>

HTML:

 <div id="id02" class="modal">
  <span onclick="document.getElementById('id02').style.display='none'" 
class="close" title="Close Modal">&times;</span>
  <!-- Modal Content -->
  <form class="modal-content animate" id="editForm" action="modify_records.php" method="post">

    <div class="container">
       <h3>Edit an Existing Project</h3>

          <label for="Project_Name" class="ui-hidden-accessible">Project Name:</label>
          <input type="text" name="Project_Name" id="Project_Name_Edit" placeholder="Project Name">
          <br><br>
          <label for="Partners" class="ui-hidden-accessible">Partners:</label>
          <?php
          echo $partnersmenu;
          ?>
         <br><br>
          <label for="blank" class="ui-hidden-accessible">. </label>
             <br><br>

          <br><br>
  <input type="button" id="edit_button" class="edit_button" value="Submit"  onclick="edit_row(<?php echo $idtemp;?>);">
          <button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>
    </div> 
    <div class="container" style="background-color:#f1f1f1">
    </div>
  </form>
</div>

Javascript:

function edit_row(id){
initialize variables
send ajax data to modify_records.php
...
}

【问题讨论】:

    标签: javascript php jquery html modal-dialog


    【解决方案1】:

    编辑:隐藏字段的解决方案:

    首先给循环中的按钮一个类

    <?php
    while ($row=mysqli_fetch_array($select)) 
    {
    $idtemp=$row['id'];
    <button class="openModal" id="edit_form<?php echo $idtemp;?>"  data-target="#id02" data-id="<?php echo $idtemp;?>"  onclick="document.getElementById('id02').style.display='block'">Edit</button>
     <?php
    }
    ?>
    

    比在你的模态中放置一个隐藏字段

    <div id="id02" class="modal">
      <span onclick="document.getElementById('id02').style.display='none'" 
    class="close" title="Close Modal">&times;</span>
      <!-- Modal Content -->
      <form class="modal-content animate" id="editForm" action="modify_records.php" method="post">
    
        <div class="container">
           <h3>Edit an Existing Project</h3>
    
              <label for="Project_Name" class="ui-hidden-accessible">Project Name:</label>
              <input type="text" name="Project_Name" id="Project_Name_Edit" placeholder="Project Name">
              <br><br>
              <label for="Partners" class="ui-hidden-accessible">Partners:</label>
              <?php
              echo $partnersmenu;
              ?>
             <br><br>
              <label for="blank" class="ui-hidden-accessible">. </label>
                 <br><br>
    
              <br><br>
      <input type="button" id="edit_button" class="edit_button" value="Submit"  onclick="edit_row(<?php echo $idtemp;?>);">
      <input type="hidden" id="hidden_temp_id">
              <button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>
        </div> 
        <div class="container" style="background-color:#f1f1f1">
        </div>
      </form>
    </div>
    

    比在你的 js 文件中为你的按钮创建事件监听器

    var openModalButtons = document.getElementsByClassName("openModal");
    for (var i = 0; i < openModalButtons.length; i++) {
        openModalButtons[i].addEventListener('click', myFunction, false);
    }
    
    var myFunction = function() {
        var idtemp = this.getAttribute("data-id");
        document.getElementById('hidden_temp_id').value = idtemp;
    };
    

    【讨论】:

    • 以前,模态的 div 在 php 循环中,因此 $idtemp 会随着循环而变化,但是一旦模态打开,循环就会重置该值。请记住,我也尝试过使用隐藏输入,我会得到相同的结果。
    • 您可以使用隐藏字段来完成。你怎么试?在您的模式中只放置一个隐藏字段,然后当您单击循环中的一个按钮时,将您的隐藏字段值替换为您按钮中的数据 ID。
    • 这听起来是一个有趣的解决方案,但是我怎样才能将隐藏字段值替换为 data-id?
    • 不幸的是,它没有拾取您分配给隐藏输入的值。 :( 不过我很感谢你的帮助。为了清楚起见,我把你的 javascript 放在了我的 edit_row 函数之前。
    • 当然,你也必须修改你的javascript函数edit_row(id)。无需传递 id,只需在没有参数的情况下调用它。在您的函数中,您从隐藏字段中获取值,因为您的 id 就在其中。
    猜你喜欢
    • 2021-01-08
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2018-01-25
    • 2014-11-13
    相关资源
    最近更新 更多