2012-07-27 22 views
0

點擊時有一個按鈕運行一個函數,我希望前一個TD中的值從no改爲yes。如何通過Javascript更改之前TD中的值

HTML:

<tr class="odd"> 
    <td class="">13.00</td> 
    <td class="">DDR</td> 
    <td class="">No</td> 
    <td class=""> 
    <div class="button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="javascript:paid(4);return false" href="#" "="" role="button" aria-disabled="false"> 
     <span class="ui-button-text">Paid</span> 
    </div> 
    </td> 
</tr> 

因此,當用戶點擊 「付費」 這個叫..使用Javascript:

function paid(iD) {  
    $.ajax({ 
     contentType: "application/json; charset=utf-8", 
     type: "POST", 
     url: "Paid", 
     dataType: "json", 
     data: "{ id:" + iD +"}", 
     success: function (id) { 
      //Here change value of previous TD to yes; 

     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) {    
     } 
    }); 
}; 

有沒有一種方法,我可以訪問以前的TD的html和改變它的價值?

O也使按鈕消失?

+0

是它總是將是最後的TD? – aztechy 2012-07-27 13:36:45

+0

不會有幾行按鈕在 – Beginner 2012-07-27 13:37:16

回答

1

這是一種可行的方法。但隨着這類事情,你需要小心後更改您的HTML:

$("span.ui-button-text").click(function(){ 
    var td = $(this).closest("td").prev(); 
    //do ajax here 
}); 

這將允許你存儲正確的td單元格,然後,你可以參考你的成功回調中。例如:

td.html("Yes"); 
+0

但我需要訪問ID – Beginner 2012-07-27 13:41:27

+0

是啊,你有什麼意見?你仍然可以以相同的方式訪問。如果你還沒有得到身份證,那麼你不會問這個問題。 – musefan 2012-07-27 13:43:04

+0

我會建議使用ID值標記div,以便您可以更自由地訪問。例如,您可以使用data-paidid =「4」'設置一個數據屬性,然後在'click'事件中用'var id = $(this).data(「paidid」)訪問javascript。 ;' – musefan 2012-07-27 13:46:14

1

這裏的關鍵是保留對外部this(點擊按鈕)的引用。一旦你輸入成功回調,上下文就會丟失,因爲它是nan操作。

雖然我們可以捕獲外部的this,但立即執行的函數將它作爲參數引用傳入。

success: (function(button) { return function (id) { 
     $(button).closest('td').prev('td').text('yes'); 
    }; })(this) 
+0

OP將想知道'that'從哪裏來。 – 2012-07-27 13:38:30

+0

重命名爲'button'以使其更具指示性。 – Utkanos 2012-07-27 13:39:23

+0

我創建了一個[小小提琴](http://jsfiddle.net/2BxxL/),這個封閉是非常好的功能解決方案。 – Stano 2012-07-27 14:06:34

0

解決方法

添加一個ID爲TR元素

<tr id="tr1" class=".odd"> 

<div class="button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="javascript:paid(4,"tr1");return false" href="#" "="" role="button" aria-disabled="false"> 

和修改您的支付功能

paid(id, rowId); 

TD作爲

<td class="yn">No</td> 

,併成功處理程序

success: function (id) { 
$("#"+rowId+" .yn").html("Yes"); 
} 

============================ ================== OLD: 簡單的辦法就是給你的是/否TD類 說YN

讓你的TD看起來

<td class="yn">No</td> 

success: function (id) { 
$(".odd .yn").html("Yes"); 
}, 
+0

但是然後所有的tds與是會改變我只想要前一個現在都改變 – Beginner 2012-07-27 13:39:32

+0

你可以通過jQuery綁定這個點擊事件嗎? 那樣我們可以得到引發該事件的TR的參考? 或者您可以將您行的ID作爲參數發送到付費功能。所以你可以像$(「#」+ rowID +「.yn」)。html(「Yes」); – AdityaParab 2012-07-27 13:42:48

0

HTML實現你的成功處理程序:

<td class="" id="paid">No</td> 

的Javascript:

document.getElementById("paid").innerHTML = "Yes"; 
+0

但是會有好幾行是,我只希望以前的td不會全部改變 – Beginner 2012-07-27 13:39:07

+0

id是唯一的,所以你可以讓你的id爲數字,然後在javascript中使用增量變量來訪問最新的一個。 – 2012-07-27 13:58:59

0

一個好方法將使用的$阿賈克斯(上下文屬性。 )保存對你想要修改的前一個TD的引用。

例如:

$.ajax({ 
     ... 
     context: prevTD, // specify the previous TD as context 
     ... 
     success: function (data) { 
      // 'this' refers to the passed in context 
      // so you can do 
      this.text('Yes'); 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) {    
     } 
    }); 

爲了得到從正在調用之前的TD,你可以使用:

yourPaidButton.parents('TD').first().prev() 
相關問題