douqihou7537 2013-03-07 16:31
浏览 46
已采纳

删除表行时,Jquery对话框关闭

I am currently trying to remove a table row from a dialog box within jQuery, but whenever I try to delete the row, the dialog box closes immediately. Currently the PHP page generates the table and rows for the dialog box. Now to note, this code works perfectly fine without the PHP page call, but by adding the PHP aspect, the dialog box fails to stay open and remove the table row.

I'm not too sure how to tackle this as my experience with jQuery is very limited and I haven't been able to find something on the internet with a similar issue. Any pointers and suggestions are welcome. The ultimate goal is to be able to post from the dialog box form to a database and any help would be appreciated.

The code has been simplified so that the mysql query is not being used, but ultimately I want to be able to pass an unordered list of data into the php page to build a dynamic query and allow the user to submit the form thereafter.

Below is the code separated into two files...

  1. PHP
   <script>
   function showTable(str, elementid, page)
   {
   if (str=="")
     {
     document.getElementById(elementid).innerHTML="";
     return;
     } 
   if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp=new XMLHttpRequest();
     }
   else
     {// code for IE6, IE5
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
   xmlhttp.onreadystatechange=function()
     {
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
       document.getElementById(elementid).innerHTML=xmlhttp.responseText;
       }
     }
   xmlhttp.open("GET",page+"?q="+str,true);
   xmlhttp.send();

   }
   </script>

   <!doctype html>
   <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>jQuery UI Dialog - Modal form</title>
      <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>


    <script type="text/javascript">
          $(function() {
            $( "#dialog" ).dialog({
              autoOpen: false,
              modal: true,
              width: 'auto',
              show: {
                effect: "blind",
                duration: 1000
              },
              hide: {
                effect: "explode",
                duration: 1000
              }
            });

            $('span.delete').on('click', function() {  
                            $(this).closest('tr').find('td').fadeOut(1000, 
                                function(){ 
                                    // alert($(this).text());
                                    $(this).parents('tr:first').remove();                    
                                });    

                            return false;
                        });


            $( "#opener" ).click(function() {
              $( "#dialog" ).dialog( "open" );
            });
          });
          </script>

</head>
<body>
    <input type=button id="opener" value="PRESS BUTTON!" onclick="showTable('NOTHING', 'dialog', '2pass.php')">

    <div id="dialog">

    </div>

</body>

  1. pass.php
    <?php           
echo "<form method = 'post'>";
echo "<table border='1' name='testTable'>
<thead>
    <tr>
        <th>Name</th>
        <th>Notes</th>
        <th>Delete</th>
    </tr>
</thead>
";

echo "<tbody>";

      echo "<tr>";
          echo "<td><input type='text' id='Name' value='Name' disabled></td>";
          echo "<td><input type='text' name='notes'></td>";
          echo '<td><span class="delete"><a href="">Delete</a></span></td>';
      echo "</tr>";

      echo "<tr>";
          echo "<td><input type='text' id='Name' value='Name' disabled></td>";
          echo "<td><input type='text' name='notes'></td>";
          echo '<td><span class="delete"><a href="">Delete</a></span></td>';
      echo "</tr>";

      echo "<tr>";
          echo "<td><input type='text' id='Name' value='Name' disabled></td>";
          echo "<td><input type='text' name='notes'></td>";
          echo '<td><span class="delete"><a href="">Delete</a></span></td>';
      echo "</tr>";

echo "</tbody>";
echo "</table>";
echo "</form>";
    ?>
  • 写回答

1条回答 默认 最新

  • duanmu3049 2013-03-07 19:08
    关注

    I figured it out. Firstly, you need to remove the show/hide components of the first function and then create a button instead of using the hyperlink reference. You will then need to set tr with an id.

    <tr id="row">
    

    Then a function in javascript to remove the row when you click the button.

       $(document.getElementById(row)).remove();
    

    After that you should be all set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题