模态框内的Ajax表单提交

时间:2015-07-04 03:09:23

标签: javascript php jquery ajax

好的,这是一个奇怪的小问题:

这是一个测试页面,用户点击打开:

enter image description here

当用户点击查看结果时,我在模态框中有3个选择框。

box1 => populate => 方框2 =>填充方框3

enter image description here

我的问题

当用户点击提交时,而不是根据选择框选择从查询中显示结果,测试页面会再次在模态框中打开...如下图所示

提交 enter image description here

知道为何提交表单当前页面在modalbox中打开

提交表单

  <script type="text/javascript">
    jQuery(document).click(function(e){
        var self = jQuery(e.target);
        if(self.is("#resultForm input[type=submit], #form-id input[type=button], #form-id button")){
            e.preventDefault();
            var form = self.closest('form'), formdata = form.serialize();
            //add the clicked button to the form data
            if(self.attr('name')){
                formdata += (formdata!=='')? '&':'';
                formdata += self.attr('name') + '=' + ((self.is('button'))? self.html(): self.val());
            }
            jQuery.ajax({
                type: "POST",
                url: form.attr("action"), 
                data: formdata, 
                 success: function(data) { $('#resultForm').append(data); }
            });
        }
    });
    </script>

填充文本框

 <script type="text/javascript">
    $(document).ready(function()
    {
     $(".sport").change(function()
     {
      var id=$(this).val();
      var dataString = 'id='+ id;

      $.ajax
      ({
       type: "POST",
       url: "get_sport.php",
       dataType : 'html',
       data: dataString,
       cache: false,
       success: function(html)
       {
          $(".tournament").html(html);
       } 
       });
      });


     $(".tournament").change(function()
     {
      var id=$(this).val();
      var dataString = 'id='+ id;

      $.ajax
      ({
       type: "POST",
       url: "get_round.php",
       data: dataString,
       cache: false,
       success: function(html)
       {
        $(".round").html(html);
       } 
       });
      });

    });
    </script>


<label>Sport :</label> 
<form method="post" id="resultForm" name="resultForm" action="result.php">
<select name="sport" class="sport">
<option selected="selected">--Select Sport--</option>
<?php
 $sql="SELECT distinct sport_type FROM events";
 $result=mysql_query($sql);
 while($row=mysql_fetch_array($result))
 {
  ?>
        <option value="<?php echo $row['sport_type']; ?>"><?php echo $row['sport_type']; ?></option>
        <?php
 } 
?>
</select>

<label>Tournamet :</label> <select name="tournament" class="tournament">
<option selected="selected">--Select Tournament--</option>
</select>

<label>Round :</label> <select name="round" class="round">
<option selected="selected">--Select Round--</option>
</select>
<input type="submit" value="View Picks" name="submit" />
</form>

<?php

显示结果

  if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

       echo $sport=$_POST['sport'];
        echo $tour=$_POST['tournament'];
        echo $round=$_POST['round'];

        $sql="Select * FROM Multiple_Picks WHERE tournament ='$tour' AND round='$round' GROUP BY member_nr";
        $result = mysql_query($sql);
        ?>
        <?php
        while($row=mysql_fetch_array($result)){
            $memNr = $row['member_nr']; 
            $pick = $row['pick'];
            $score = $row['score'];
            ?>

            echo $memNr;
            echo $pick;
            echo $score;
        }

    }

    ?>

1 个答案:

答案 0 :(得分:1)

看来:

success: function(data) { $('#resultForm').append(data); }你告诉它把ajax响应放在resultForm中,它似乎在你的模态中。这不是正在发生的事情。很难从你的问题和代码中分辨出应该发生什么,而不是现在发生的事情。