2016-01-26 95 views
-1

我有jQuery中下面的代碼爲什麼var沒有更新?

var fila; 
$(document).ready(function(){ 
    acciones_filas('tbl_producto', 'acciones-activos', productoTable); 
    $("#id_producto").val(fila.nProCodigo); 
} 

function acciones_filas(tabla_id, acciones_id, tabla_objeto, fila) 
{ 
    //click on a row 
    $("#"+tabla_id+" tbody").on('click', 'tr', function(){ 
     $("#"+acciones_id+" .btn").removeAttr('disabled'); 
     if ($(this).hasClass('row_selected')) { 
      $(this).removeClass('row_selected'); 
     } 
     else { 
      tabla_objeto.$('tr.row_selected').removeClass('row_selected'); 
      $(this).addClass('row_selected'); 
     } 

     if($('#'+tabla_id+' .row_selected').length < 1) 
      $("#"+acciones_id+" button").attr('disabled', 'disabled'); 

     fila = tabla_objeto.fnGetData(this); 
    }); 
} 

當我在一個表中的變量fila一行單擊要保存選定行的值,但是當我打印出結果我得到和undefined

+3

不包括'在acciones_filas'的'參數列表fila',它會成爲一個局部變量,並在影子全球'fila'功能。 – Teemu

+0

對不起,我忘了刪除它。 – laviku

+0

@Teemu解決了它,謝謝 – laviku

回答

2

在你的情況下,你有兩個增值稅fila。一個在功能裏面,另一個在外面。 JavaScript會嘗試使用更接近的那個。在這種情況下,函數acciones_filas的參數。

給對鏈接點8看看:What is the scope of variables in JavaScript?

相關問題