函数为未定义错误

Function is undefined error

本文关键字:错误 未定义 函数      更新时间:2024-01-12

我正在尝试调用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");
            });
        }   
});

我该怎么办?

因此,在文档准备好后,jQuery函数中的代码将无法直接访问,您可以将函数移出,也可以调用其中的函数:

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

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

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

尝试用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");
            });
        }