将按钮元素传递给回调函数?

时间:2020-12-29 02:04:54

标签: javascript jquery callback

我正在尝试在第一个函数完成后将一个按钮元素传递给第二个函数。 我已经使用调试器工具逐步完成了它,并且传递到回调函数中的 ele 按钮被正确传递。 但是,在 runThisSecond 中传递的按钮元素返回 undefined。关于为什么会这样的任何想法?

HTML 文件:


<button type="button" class="btn fruitbtn" data-fruitname= "@item.FruitName">   Add Fruit </button>

JavaScript:


$("$table#Results").on('click', '.fruitbtn', function() {
  runThisSecond(runThisFirst($(this)));
})
    
function runThisFirst (ele, callback) {
        swal({ 
            html:true,
            title: "Fruits"
        },
            function(isConfirm) {
                callback(ele);    //button element is correctly passed in here
            }
        )
    }
    
    function runThisSecond(ele) {
        var button = ele;    //This is returning undefined
    }

1 个答案:

答案 0 :(得分:1)

您当前传递给 runThisSecond 的唯一内容是从 runThisFirst 返回的值,它什么都没有 (undefined)。如果您希望 runThisSecondcallback 中充当 runThisFirst 参数,则需要将其作为第二个参数传递:

runThisFirst($(this), runThisSecond)
相关问题