gpt4 book ai didi

javascript - 无法在 JS 函数之间传递变量

转载 作者:行者123 更新时间:2023-11-29 20:58:34 24 4
gpt4 key购买 nike

我遇到的问题是“the_id”变量需要传递给“deleteEntry()”函数。我能够成功传递一个没有引号的整数,但我无法将任何变量传递给函数。尝试在引号中或不在引号中传递变量时出现的错误是 - “未捕获的 ReferenceError:the_id 未在 HTMLInputElement.onclick 中定义”。关于问题出在哪里的任何建议?提前致谢。

        var the_id = $(this).parents('tr:first').find('td:nth(4)').text();
the_id = parseInt(the_id);
$("#indate").remove();
newDate = document.createElement("input");
newDate.setAttribute("type","text");
newDate.setAttribute("id","indate");
newDate.setAttribute("placeholder",dateText);
newDate.setAttribute("onfocus","(this.type='date')");
newDate.setAttribute("onblur","(this.type='text')");
$("#changeDateType").append(newDate);
$("#description").attr("placeholder",descriptionText);
$("#inamount").attr("placeholder",amountText);
if(bill == "1"){
$("#billcheck").prop("checked",true);
}
else {
$("#billcheck").prop("checked",false);
}
$("#submitBill").remove();
$("#deleteBill").remove();
$('#submitBillInfo').append('<input type="submit" id="submitBill" value="Submit" onclick="submitEntry()">');
$('#submitBillInfo').append('<input type="submit" id="deleteBill" value="Delete" onclick="deleteEntry(the_id)">');
$("#billForm").removeAttr("onsubmit");
}
}

function deleteEntry(nums){
alert(nums);
var c = confirm("Do you want to delete this item?");
if( c == true){
$(function(){
$.ajax({
type: 'POST',
url: '../Js/deleteData.php',
data: { data: nums },
});
});

最佳答案

因为您在函数体内定义了“the_id”变量,所以您不能从 HTML DOM 访问它。因此,当您单击删除按钮时,它会返回错误。

我认为您可以解决此问题,分配一个事件监听器以在附加输入字符串后删除输入。

首先,删除您插入的删除按钮上的 onclick 声明。

$('#submitBillInfo').append('<input type="submit" id="deleteBill" value="Delete">');

然后为删除按钮添加一个事件监听器:

$('#deleteBill').click(function(){ deleteEntry(the_id); })

关于javascript - 无法在 JS 函数之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47917544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com