Javascript无法获取AJAX MODAL形式的变量的值

问题描述 投票:2回答:1

我对AJAX还是陌生的,并在youtube上关注了一些教程,但是我在某些方面仍然停留。所以我只是在这里改变了一些东西,并根据我的理解进行了调整。

此按钮将在表上获得用户想要批准的项目ID:

<button type="submit" name="approve" id="<?php echo $displayapprovaldetails['projectid'];?>" class="btn btn-primary btn-sm approve_data">Approve</button>

现在,我有此模式表单,该表单将在批准和更新与projectid相关的记录之前先要求备注:

<div class="modal fade" id="approve_data_Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title font-weight-bold text-primary" id="exampleModalLabel">Approve</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <form method="post">
        <div class="modal-body">
            <label>Remarks: </label>
            <textarea name="remarks" id="remarks" rows="5" class="form-control"></textarea>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
          <button class="btn btn-primary" type="submit" name="approve" id="approve">Confirm</button>
          </form>
        </div>
      </div>
    </div>

我更改了脚本以获取两个嵌套函数的变量,但是它仍然无法正常工作。我不确定我的脚本是否也正确。

脚本:

<script>
$(document).ready(function(){

  $(document).on('click', '.approve_data', function(){
     var projectid = $(this).attr("projectid");

     $('#approve_data_Modal').modal('show');
     $(document).on('click', '#approve', function(){
      var remarks = $("#remarks").val();
      var projectid = $projectid;

      if(remarks=="")
      {
        $("#lblRemarks").html("What do you want to say?");
      }

     $.ajax({
        url:"include/approve.php",
        type:"post",
        data:{projectid:projectid, remarks:remarks},
        success:function(data){
          $("#approve_data_Modal").modal('hide');
          }
        });
     });
  });
});
</script>

[有人可以帮助我查明我的脚本出了什么问题以及如何更正它以获取两个变量($ projectid和$ remarks)吗?

谢谢。

php jquery ajax bootstrap-modal
1个回答
0
投票

而不是有一个按钮=

<button type="submit" name="approve" id="<?php echo $displayapprovaldetails['projectid'];?>" data-toggle="modal" data-target="#approve_data_Modal" class="btn btn-primary btn-sm">Approve</button>

使用输入=

<input type='text' id="project_id" value="<?=$displayapprovaldetails['projectid']?>" >

您也可以使用按钮进行提交,但是在向其他文件发送ID时,我建议您这样做

您的HTML:

<div class="modal fade" id="approve_data_Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title font-weight-bold text-primary" id="exampleModalLabel">Approve</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <form method="post" id="approve_form">
        <div class="modal-body">
            <label>Remarks: </label>
            <textarea name="remarks" id="remarks" rows="5" class="form-control"></textarea>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
          <button class="btn btn-primary" type="submit" name="approve" id="approve">Confirm</button>
          </form>
        </div>
      </div>
    </div>

这是您的脚本文件:

    <script>
    $(document).ready(function(){
      $('#approve').on('submit', function(event){
            event.preventDefault();
 var id = $("#project_id").val();
            if($('#remarks').val() == "")
            {
              alert("Remark is required.");
            }
            else
            {
              $.ajax({
                url:"include/approve.php",
                method:"post",
                data:{id:id},
                success: function(data)
                {
                  $('#approve_form')[0].reset();
                  $('#approve_data_Modal').modal('hide');
                }
              });
            }
        });
    });
    </script>
© www.soinside.com 2019 - 2024. All rights reserved.