2016-04-06 164 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());}); } –