将按钮元素传递给回调函数?
Pass a button element into callback function?
我试图在第一个函数完成后将按钮元素传递给第二个函数。
我已经使用调试器工具逐步完成它,并且传递给回调函数的 ele 按钮被正确传递。
但是,在 runThisSecond 中传递的按钮元素返回未定义。关于为什么会这样有什么想法吗?
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
}
您目前唯一传递给 runThisSecond
的是从 runThisFirst
返回的值,它什么也不是 (undefined
)。如果您希望 runThisSecond
作为 runThisFirst
中的 callback
参数,则需要将其作为第二个参数传递:
runThisFirst($(this), runThisSecond)
我试图在第一个函数完成后将按钮元素传递给第二个函数。 我已经使用调试器工具逐步完成它,并且传递给回调函数的 ele 按钮被正确传递。 但是,在 runThisSecond 中传递的按钮元素返回未定义。关于为什么会这样有什么想法吗?
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
}
您目前唯一传递给 runThisSecond
的是从 runThisFirst
返回的值,它什么也不是 (undefined
)。如果您希望 runThisSecond
作为 runThisFirst
中的 callback
参数,则需要将其作为第二个参数传递:
runThisFirst($(this), runThisSecond)