2013-03-01 119 views
0

我正在關注本教程,所以我的問題是: 我在這裏有這個代碼,這是一些表。現在使用jquery和ajax傳遞變量php

<tr id="<?php echo $id; ?>" class="edit_tr"> 

<td class="edit_td"> 
<span id="first_<?php echo $id; ?>" class="text"><?php echo $kodi; ?></span> 
<input type="text" value="<?php echo $kodi; ?>" class="editbox" id="first_input_<?php echo $id; ?>" /&gt; 
</td> 

<td class="edit_td"> 
<span id="last_<?php echo $id; ?>" class="text"><?php echo $vlera; ?></span> 
<input type="text" value="<?php echo $vlera; ?>" class="editbox" id="last_input_<?php echo $id; ?>"/> 
</td> 

</tr> 

,這是腳本:

<script type="text/javascript"> 
$(document).ready(function() 
{ 
$(".edit_tr").click(function() 
{ 
var ID=$(this).attr('id'); 
$("#first_"+ID).hide(); 
$("#last_"+ID).hide(); 
$("#first_input_"+ID).show(); 
$("#last_input_"+ID).show(); 
}).change(function() 
{ 
var ID=$(this).attr('id'); 
var kodi=$("#first_input_"+ID).val(); 
var vlera=$("#last_input_"+ID).val(); 
var dataString = 'id='+ ID +'&kodi='+kodi+'&vlera='+vlera; 
$("#first_"+ID).html('<img src="load.gif" />'); // Loading image 

if(first.length>0&& last.length>0) 
{ 

$.ajax({ 
type: "POST", 
url: "table_edit_ajax.php", 
data: dataString, 
cache: false, 
success: function(html) 
{ 
$("#first_"+ID).html(first); 
$("#last_"+ID).html(last); 

我不是傳遞變量權maybe..because它不會保存,查詢不會excecute。 感謝

+0

你得到什麼錯誤? – 2013-03-01 13:47:11

+0

沒有錯誤,它只是不會更新值 – pyetjegoo 2013-03-01 13:47:31

+0

你可以使用像Firebug的東西?它會告訴你ajax請求,然後你可以看到你通過郵件傳遞了什麼。 – Cito 2013-03-01 14:21:10

回答

0

jQuery中最簡單的方式來傳遞你的表單字段Ajax是通過使用serialize

data: $("form").serialize(); 

另外一個問題是,您使用的是兩個變量,firstlast,但你是不是將它們定義任何地方。

+0

嗯,我無法正確修改這一個,我很難做這樣的事情.. – pyetjegoo 2013-03-01 13:49:11

+0

@pyetjegoo看我的編輯。 – jeroen 2013-03-01 13:57:02

0

試試這個代碼:

$(function() { // This makes the same than $(document).ready(); 
    $(".edit_tr").each(function() { // Instead of assign the click event, we loop through each element 
     var $element = $(this); 
     var id = this.id; 
     var $first = $('#first_' + id); 
     var $last = $('#last_' + id); 
     var $firstInput = $('#first_input_' + id); 
     var $lastInput = $('#last_input_' + id); 
     $element.click(function() { 
      $first.hide();$last.hide(); 
      $firstInput.show();$lastInput.show(); 
     }).change(function() { 
      $first.html('<img src="load.gif" />'); 
      // Here you have an if, but those elements are not defined in the script... 
      // if (first.length > 0 && last.length > 0) { 
      $.post("table_edit_ajax.php", { 
       id : id, 
       kodi : $firstInput.val(), 
       vlera : $lastInput.val() 
      }, function(html) { 
       // Here again the two vars not defined 
       //$first.html(first); 
       //$last.html(last); 
      }); 
      // } 
     }); 
    }); 
});