我有这个调用模态的函数
$('#btn1').click(function () {
// Get textfield
var textfield = $('#txt1');
// Get value
var input = textfield.val();
// Check if the value is already in the database
var json = textfield.data('json');
if (itemExists(input, json)) {
// Stuff
} else {
$('#modalAddText').append('X does not exist');
$('#myModal').modal();
}
});
然后检查用户是否单击“确定”
$('#modalOk').click(function () {
insertItem(itemone, itemtwo);
$('#myModal').modal('hide');
});
所以问题是我无法再访问第一个函数中的文本字段,那么我如何获取/保持对它的访问?
我不能只做 $('#txt1');
因为我有多个 txtfields
那么我可以以某种方式传输 textfield
变量吗?
最佳答案
尝试在外部作用域中声明textfield
。
即:
var textfield = $('#txt1');
$('#btn1').click(function () {
... // textfield is achievable
});
$('#modalOk').click(function () {
... // textfield is achievable
});
关于javascript - 如何在 jQuery 中传输元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28516263/