2016-04-06 166 views
0

我想在給onchange文本框的onchange中調用一個函數。每當我將文本框放大時,都應該調用該函數。但我越來越myFunction(函數名稱)是未定義的。這就是我所做的:功能未定義錯誤

var c = 0; 
    jQuery(document).ready(function() { 
     jQuery("#table_projection_value").dataTable({ 
       "sAjaxSource": "includes/inc-projection-db.php?mode=projection_dataTable", 
       "bDestroy": true, 
       "bPaginate": false, 
       "bInfo": false, 
       "bFilter": false, 
       "bSort": false, 
       "aoColumnDefs": [ 
        { 
         "aTargets": [0], 
         "mRender": function(data, type, row) { 
          return data + '<input type="hidden" class="user_id" name="user_id[]" id="user_id" value="' + row[4] + '">'; 
         } 
        }, 
        { 
         "aTargets": [1], 
         "mRender": function(data, type, row) { 
          return '<input type="text" onchange="myFunction();" class="form-control text-right projected_value" name="projected_value[]" id="projected_value_' + c + '_' + data + '" >'; 
         } 
        } 
       ], 
       "fnCreatedRow": function(nRow, aData, iDisplayIndex, iDisplayIndexFull){ 
        c = c + 1; 
       } 
      }); 

     function myFunction(){ 
      $(":text").blur(function() { 
      alert("**"); 
      var element=$(this); // you can get the element here 
      }); 


      $(":text").focusout(function() { 
      alert(this.id + " focus out"); 
      }); 
     } 
}); 

我該怎麼辦?

回答

1

所以jQuery函數中的代碼不會直接accessable到你之後的文件已經準備好,你要麼移動功能離開那裏,或致電功能有:

jQuery(document).ready(function() { 
    myFunc() { 
    //code 
    } 
    // call here 
}) 

jQuery(document).ready(function() { 
    //code 
}) 


    myFunc() { 
    //code 
    } 
    // call here 
+0

感謝花花公子,對你有所幫助。我知道了。但是現在,我面臨另一個問題。我試圖獲取該文本框的值,我得到「TypeError:this.id.val不是函數」錯誤。如何解決這個問題並獲得價值。這是我所做的: –

+0

function myFunction(){ \t \t \t alert(「Coming!」); \t \t \t $( 「:文本」)。模糊(函數(){ \t \t \t \t \t \t VAR元= $(本); //你可以得到元素在這裏 \t \t \t}); \t \t \t $( 「:文本」)。事件的內容(函數(){ \t \t \t警報(this.id + 「集中了」); \t \t \t警報(( 「#this.id」) .val()); \t \t \t // alert((this).val()); \t \t \t}); \t \t} –

1

試着寫你的函數了jQuery(document).ready(function() {...})

jQuery(document).ready(function() {...}); 
function myFunction(){ 
      $(":text").blur(function() { 
      alert("**"); 
      var element=$(this); // you can get the element here 
      }); 


      $(":text").focusout(function() { 
      alert(this.id + " focus out"); 
      }); 
     } 
+0

我試過了。那個錯誤消失了,但我在那個onchange沒有得到那個警報。爲什麼? –

+0

謝謝兄弟,爲您提供幫助。我知道了。但是現在,我面臨另一個問題。我試圖獲取該文本框的值,我得到「TypeError:this.id.val不是函數」錯誤。如何解決這個問題並獲得價值。這就是我所做的: –

+0

function myFunction(){alert(「Coming!」); $(「:text」)。blur(function(){var element = $(this); //你可以在這裏獲得元素}); $(「:text」)。focusout(function(){alert(this.id +「focus out」); alert((「#this.id」)。val()); // alert((this)。 val());}); } –