2017-06-28 98 views
-4

這是我的javascript代碼,事件是onkeyup多行表!如何在JavaScript中定義一個函數的全局變量?

function product(id) 
    { 
     $('#customers2').find('tr').click(function(){ 
      var clicked = this; 
     }); 
      var row = $(clicked).find('#tempnow').val(); 
      if (row == "noncheck"){ 
       $(clicked).find('#ett').val(""); 
       $(clicked).find('#ett').prop("disabled", true); 
      } 
      else{ 
       $(clicked).find('#ett').prop("disabled", false); 
       $.ajax({ 
        url:baseurl+"/getinform/", 
        type: "POST", 
        dataType: "json", 
        data: {masp : id}, 
        success:function(data) {  
         var thue = data['pcs_cl_pd_thue']; 
         $(clicked).find('#tyu').css("color", "red"); 
         $(clicked).find('#tyu').val(thue);    
        } 
       }); 
      } 

    } 

當我使用螢火蟲,變量點擊沒有定義?

+3

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Function_scope – Teemu

回答

1
var clicked; // Define it outside 

$('#customers2').find('tr').click(function(){ 
    clicked = this; // Give it a value on click, from inside a function 
    next() // Now it has a value, call another function that'll do something with it 
}); 

function next(){ 
    $(clicked).find('#ett') // It works! 
} 
相關問題