2011-03-30 72 views
0

您好我是Jquery的新手。我想添加兩個變量。兩者都是可悲的變數。 請參閱本代碼末尾的「添加變量」。 「cashupfirst」變量顯示值正確,但「cashupsecond」變量顯示「未定義」。但是我可以在此代碼之前顯示「cashupsecond」的正確值。請幫忙。無法在Jquery中添加兩個變量

jQuery(function($) { 

    var cashupfirst, cashupsecond; 
    var a, parent, input, doneLink, b, i, eq, c, z; 
    $(".Cashups").delegate("td:eq(3) > a", "click", function(event) { 

    //Index reading(not used here) 
    //var eq = $(this).parent().children("a").index(this); 


    // The `a` has been clicked; cancel the action as 
    // we're handling it 
    event.preventDefault(); 
    event.stopPropagation(); 

    // Remember it and its parent 
    a = $(this); 
    parent = a.parent(); 

    // Insert an input in front of it, along with a done link 
    // because blur can be problematic 
    input = $("<input type='text' size='10'>"); 
    input.insertBefore(a); 
    input.blur(doneHandler); 
    doneLink = $("<a href='#'>done</a>"); 
    doneLink.insertBefore(a); 
    doneLink.click(doneHandler); 

    // Put the text of the link in the input 
    input.val(a.text()); 


    // Temporarily detach the link from the DOM to get it 
    // out of the way 
    a.detach(); 

    // Give the input focus, then wait for it to blur 
    input[0].focus(); 

    // Our "done" handler 
    function doneHandler() { 
     // Replace the content of the original link with 
     // the updated content 
     a.text(input.val()); 

     cashupfirst = a.text(); 
     alert(cashupfirst); 
     //Set cookie to pass the text value to update it to table permanently 
     $.cookie('demoCookie',cashupfirst,{expires: 7, path: '/'});  

     // Put the link back, remove our input and other link 
     a.insertBefore(input); 
     input.remove(); 
     doneLink.remove(); 

     }  

    }); 



    $(".Cashups").delegate("td:eq(9) > a", "click", function(event) { 

    // The `a` has been clicked; cancel the action as 
    // we're handling it 
    event.preventDefault(); 
    event.stopPropagation(); 

    // Remember it and its parent 
    a = $(this); 
    parent = a.parent(); 

    // Insert an input in front of it, along with a done link 
    // because blur can be problematic 
    input = $("<input type='text' size='10'>"); 
    input.insertBefore(a); 
    input.blur(doneHandler); 
    doneLink = $("<a href='#'>done</a>"); 
    doneLink.insertBefore(a); 
    doneLink.click(doneHandler); 

    // Put the text of the link in the input 
    input.val(a.text()); 


    // Temporarily detach the link from the DOM to get it 
    // out of the way 
    a.detach(); 

    // Give the input focus, then wait for it to blur 
    input[0].focus(); 

    // Our "done" handler 
    function doneHandler() { 
     // Replace the content of the original link with 
     // the updated content 
     a.text(input.val());  
     cashupsecond = a.text(); 
     alert(cashupsecond); 

     //Set cookie to pass the text value to update it to table permanently 
     $.cookie('demoCookie1',cashupsecond,{expires: 7, path: '/'}); 


     // Put the link back, remove our input and other link 
     a.insertBefore(input); 
     input.remove(); 
     doneLink.remove(); 

    } 

    //Add the variables 

      if (cashupfirst!= '' && cashupsecond!= '') { 
     alert(cashupfirst); 
       alert(cashupsecond); 
     var xyz = (parseInt(cashupfirst) + parseInt(cashupfirst)); 
      jQuery('td#cashcalculator_total a').html(xyz); 
    } 

    }); 
    //alert(cashupsecond); 


}); 

回答

1

嘗試

var xyz = parseInt(cashupfirst) + parseInt(cashupsecond); 

alert函數不返回什麼被驚動。

編輯:

好吧,我明白了什麼是錯的。您必須將變量添加到第二個doneHandler函數中,否則它不會等到您收到Cashup秒的值:

// Our "done" handler 
function doneHandler() { 
    ... 

    //Add the variables 
    if (cashupfirst!= '' && cashupsecond!= '') { 
     alert(cashupfirst); 
     alert(cashupsecond); 
     var xyz = parseInt(cashupfirst) + parseInt(cashupsecond); 
     jQuery('td#cashcalculator_total a').html(xyz); 
    } 
} 

// Don't add the variables here. 
+0

對不起,這是一個錯字。問題是cashupsecond是未定義的。我不知道爲什麼。謝謝。 – user679460 2011-03-30 05:46:50