【问题标题】:Call a function from Dynamically added html code using jquery使用 jquery 从动态添加的 html 代码中调用函数
【发布时间】:2013-10-25 09:15:05
【问题描述】:

这是场景

$(document).ready(function(){
 var rowIndex = 0;
 var count = 0;
 function callThisFunction(index, value, count) {
 alert('called');
}

function dynamicContentAdd() {
 rowIndex++;
 count++;

 var row = "<input name='input["+rowIndex+"]' onkeyup = 'callThisFunction("+rowIndex+","+total[1]+","+count+");' id='input"+rowIndex+"'  type='text' class='inputfield' />";

 $("#table").append(row);
}

我在单击按钮时调用了函数 dynamicContentAdd() ,它工作正常。但不起作用的是它没有在 keyup 上调用函数 callThisFunction() 。它给出了函数未定义的错误。但是当我在外部 js 文件中有相同的函数时,它会成功调用它。这不是从jquery中动态添加的html代码调用函数的方式吗?

请告诉我。

谢谢

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    问题在于,由于您使用的是内联事件处理程序,js 引擎将在全局范围内查找函数 callThisFunction,但是您已在 dom 就绪处理程序中添加了该函数,使其成为 dom 就绪处理程序的本地函数这将导致js抛出错误。

    解决方案 1. 使函数全局化

    //since the function is define outside of dom ready handler it is available in the global scope
    function callThisFunction(index, value, count) {
        alert('called');
    }
    
    $(document).ready(function () {
        var rowIndex = 0;
        var count = 0;
    
        function dynamicContentAdd() {
            rowIndex++;
            count++;
    
            var row = "<input name='input[" + rowIndex + "]' onkeyup = 'callThisFunction(" + rowIndex + "," + total[1] + "," + count + ");' id='input" + rowIndex + "'  type='text' class='inputfield' />";
    
            $("#table").append(row);
        }
    })
    

    $(document).ready(function () {
        var rowIndex = 0;
        var count = 0;
    
        //define the function as a property of the window object again making it available in the public scope
        window.callThisFunction = function (index, value, count) {
            alert('called');
        }
    
        function dynamicContentAdd() {
            rowIndex++;
            count++;
    
            var row = "<input name='input[" + rowIndex + "]' onkeyup = 'callThisFunction(" + rowIndex + "," + total[1] + "," + count + ");' id='input" + rowIndex + "'  type='text' class='inputfield' />";
    
            $("#table").append(row);
        }
    })
    

    解决方案 2:jQuery 方式 - 使用带有 data-* 属性的委托事件处理程序

    $(document).ready(function () {
        var rowIndex = 0;
        var count = 0;
    
        $('#table').on('keyup',  '.myfncaller', function(){
            var $this = $(this);
            var index = $this.data('index'), value = $this.data('value'), count = $this.data('count');
        })
    
        function dynamicContentAdd() {
            rowIndex++;
            count++;
    
            var row = "<input name='input[" + rowIndex + "]' id='input" + rowIndex + "'  type='text' class='inputfield myfncaller' data-index='" + rowIndex + "' data-value='" + total[1] + "' data-count='" + count + "' />";
    
            $("#table").append(row);
        }
    })
    

    【讨论】:

    • 谢谢。它确实奏效了。我选择了Solution1的第二个选项
    • 哇,这已经奏效了。好吧,我学到了一些新东西,谢谢@Arun P Johny
    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 2013-10-01
    • 2015-01-16
    • 2016-09-13
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多