Javascript函数被发送了不正确的值,即使html显示它是正确的

Javascript function being sent incorrect value even though the html shows it as being correct

本文关键字:html 即使 显示 函数 不正确 Javascript      更新时间:2023-09-26

我有两个javascript函数,insertAddedSup和removeAdded。当我从表中检索值并用这些信息填充控件时,onclick事件会插入用户,并且"Remove"按钮工作正常。当我使用LDAP搜索用户,并单击该用户时,所有html都是正确的(显示发送到remove函数的正确参数,就像SQL结果一样),但当我单击remove按钮时,函数中使用的参数值与html中显示的值不同。我不认识任何地方的价值。。。它看起来几乎是随机的,尽管它总是相同的值。不知道该怎么办。有人知道问题出在哪里吗?

以下是功能:

function insertAddedSup(uidSup, fullnameSup) {
    var fullnameSup = fullnameSup.toLowerCase();
    var currentContentSup = '<div id="y_' + uidSup + '" class="selectedDataBox" style="overflow:hidden;"><span style="text-transform:capitalize; float:left">' + fullnameSup + '</span><input type="hidden" name="EmpID" id="personAddedSup_' + uidSup + '" value="' + uidSup + '"><input type="button" name="removeAdded_' + uidSup + '" id="removeAdded_' + uidSup + '" value="Remove" onclick="alert(' + uidSup +');removeAdded(' + uidSup + ');" style="font-size:0.7em; float:right; width:60px;"></div>';
    $('#addedPerson').html(currentContentSup);
    alert($('#addedPerson').html());
    $('#addedPerson').show();
    $('#MainContent_searchContent_btnSubmitEmployee').attr('disabled', false);
}
function removeAdded(uidSup) { // hide the div of the person added
    var divID = "#y_" + uidSup;
    alert($('#addedPerson').html());
    $(divID).hide();
    $(divID).remove();
    if ($('#addedPeople').html() == '') {
        $('#addedPeople').hide();
    }
    $('#MainContent_searchContent_btnSubmitEmployee').attr('disabled', true);
}

我添加了一些警报,这样我就可以看到实际发送的内容、html中的内容等。

编辑:HTML

<div id="y_0730337" class="selectedDataBox" style="overflow:hidden;">
    <span style="text-transform:capitalize; float:left"></span>
    <input name="EmpID" id="personAddedSup_0730337" value="0730337" type="hidden">
    <input name="removeAdded_0730337" id="removeAdded_0730337" value="Remove" onclick="alert(0730337);removeAdded(0730337);" style="font-size:0.7em; float:right; width:60px;" type="button">
</div>

如果数字不是以0开头,它似乎也可以正常工作。有没有什么方法可以确保该值被视为字符串?

在按钮中单击:

onclick="alert(0730337);removeAdded(0730337);"

这些被视为数字而非字符串,因为参数周围没有引号。如果一个数字以0开头,那么这个数字将真正以下一个非零数字开始(在本例中为7),使值通过730337(老实说,我不知道为什么你所看到的实际通过值有如此大的不同。我很想听听其他人是否对此有解释)。

你真正想要的是:

onclick="alert('0730337');removeAdded('0730337');"

你说的数字不是以0开头的,这让我相信这就是你遇到这个问题的原因。