jQuery Datatable:编辑按钮

时间:2012-05-26 13:41:11

标签: php javascript jquery mysql edit

我在Edit添加了jQuery Datatable按钮。当用户单击此按钮时,该行将变为可编辑状态。更新的行应保存到MySQL DB,但问题出在这里 - 更新的行不会保存到DB。 Firebug不会显示任何错误消息。有人可以帮我解决问题吗?

<script type="text/javascript" charset="utf-8">
         $(document).ready(function(){
              $('#newspaper-b').dataTable({
              "sPaginationType":"full_numbers",
              "aaSorting":[[3, "asc"]],
              "bJQueryUI":true
              });
             $(".edit_but").click(function() {
              var ID=$(this).attr('id');
              $("#first_"+ID).hide();
              $("#last_"+ID).hide();
              $("#first_input_"+ID).show();
              $("#last_input_"+ID).show();
          });
             $(".edit_tr").change(function() {
                  var ID=$(this).attr('id');
                  var first=$("#first_input_"+ID).val();
                  var last=$("#last_input_"+ID).val();
                  var dataString = 'flightNum='+ ID +'&from='+first+'&STADate='+last;
                  $("#first_"+ID).html('<img src="load.gif" />'); // Loading image
                  if(first.length>0&& last.length>0) {
                  $.ajax({
                      type: "POST",
                      url: "callpage.php?page=tables/edit.php",
                      data: dataString,
                      cache: false,
                      success: function(html) {
                              $("#first_"+ID).html(first);
                              $("#last_"+ID).html(last);
                      }
                  });
                  } else
                  {
                    alert('All fields must be filled out.');
                  }
                });
              // Edit input box click action
              $(".editbox").mouseup(function() {
                return false
              });

              // Outside click action
              $(document).mouseup(function() {
                  $(".editbox").hide();
                  $(".text").show();
              });

              $("tr").click(function(){
                $(this).addClass("selected").siblings().removeClass("selected");
              });
     });
</script>

                <table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
                    <thead>
                        <tr>
                            <th scope="col">Flt Num</th>
                            <th scope="col">From</th>
                            <th scope="col">STA Date</th>
                            <th scope="col"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($result1 as $row):
                            $flightNum=$row['flightNum'];
                            $from=$row['frm'];
                            $STADate=$row['STADate'];
                        ?>
                        <tr id="<?php echo $flightNum; ?>" class="edit_tr">
                                                        <td><?php echo $row['flightNum'];?></td>
                            <td class="edit_td">
                                <span id="first_<?php echo $flightNum; ?>" class="text">
                                    <?php echo $from;?>
                                </span>
                                <input type="text" value="<?php echo $from;?>" 
                                       class="editbox" id="first_input_<?php echo $flightNum; ?>"/>
                            </td>
                            <td class="edit_td">
                                <span id="last_<?php echo $flightNum; ?>" class="text">
                                    <?php echo $STADate; ?>
                                </span> 
                                <input type="text" value="<?php echo $STADate; ?>" 
                                       class="editbox" id="last_input_<?php echo $flightNum; ?>"/>
                            </td>
                                                        <td class="edit_td"><?php echo $row['pkID']; ?></td>
                            <td id="<?php echo $flightNum; ?>" class="edit_but">
                                <div>
                                    <img src='images/edit.png' alt='Edit' />
                                </div>
                            </td>
                        </tr>
                        <?php endforeach;?>
                    </tbody>
                </table>    

edit.php

<?php
    include_once 'include/DatabaseConnector.php';
    if(isset($_POST['flightNum'])) {
        $flightnum=$_POST['flightNum'];
        $from=$_POST['from'];
        $STADate=$_POST['STADate'];
        $query = 'UPDATE flightschedule 
                  SET frm="'.$from.'",STADate="'.$STADate.'" 
                  WHERE flightNum="'.$flightNum.'"';
        DatabaseConnector::ExecuteQuery($query);
        echo '1';
    } else { 
        echo '0'; 
    }
?>

1 个答案:

答案 0 :(得分:0)

尝试$(&#34; .edit_tr输入&#34;)。change()作为触发器。

更改事件仅限于输入,选择和textareas,但您已尝试将其绑定到表格行。上面的代码将它绑定到类中行内的任何输入。