简体   繁体   English

即使html将其显示为正确值,也向Javascript函数发送了不正确的值

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

I have two javascript functions, insertAddedSup and removeAdded. 我有两个JavaScript函数,insertAddedSup和removeAdded。 When I retrieve values from a table and populate a control with this information, the onclick event inserts the user and the "Remove" button works fine. 当我从表中检索值并使用此信息填充控件时,onclick事件将插入用户,并且“删除”按钮可以正常工作。 When I use LDAP to search for a user, and I click on the user, all of the html is correct (shows the correct parameters being sent to the remove function - just like the SQL result), but when I click on the remove button, the value of the parameter being used in the function is different from that which shows up in the html. 当我使用LDAP搜索用户并单击用户时,所有html都是正确的(显示正确的参数正发送到删除功能-就像SQL结果一样),但是当我单击删除按钮时,函数中使用的参数值与html中显示的值不同。 I don't recognize the value from anywhere... it seems almost random, though it is always the same value. 我无法从任何地方识别出该值……尽管它始终是相同的值,但似乎几乎是随机的。 Not sure what to do. 不知道该怎么办。 Does anyone know what might be the problem? 有谁知道可能是什么问题?

Here are the functions: 功能如下:

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);
}

I added some alerts so that I can see what is actually being sent, what is in the html, etc. 我添加了一些警报,以便可以看到实际发送的内容,html中的内容等等。

Edit: 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>

It also seems to work ok if the number does not start with a 0. Is there some way that I can make sure that the value is treated as a string? 如果数字不是以0开头,它似乎也可以正常工作。是否可以通过某种方式确保将值视为字符串?

In your button onclick: 在您的按钮上单击:

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

These are being treated as numbers and not strings because you don't have quotation marks around the parameters. 这些被视为数字而不是字符串,因为您在参数周围没有引号。 If a number begins with a 0 the number will really begin with the next non zero number (in this case 7) making the value passed 730337 (I honestly don't know why what you see as the actual value passed is so vastly different though. I'd be interested in hearing if someone else had an explanation for this). 如果一个数字以0开头,那么该数字实际上将以下一个非零数字(在本例中为7)开头,从而使值传递了730337(老实说,我不知道为什么您看到的实际值却如此大地不同我很想听听其他人对此是否有解释)。

What you really want is: 您真正想要的是:

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

You saying numbers that don't begin with a 0 work make me believe that this is why you are having this problem. 您说的不是以0开头的数字使我相信这就是您遇到此问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方法即使在那儿也找不到 - Method Not being found even though it is there 即使发送了请求,ASP.NET MVC 表单数据也不会保存到数据库中 - ASP.NET MVC Form data is not being saved into database even though the request is sent 即使存在于数据库中也找不到角色 - Role not being found even though exists in the database 即使正在使用Dispatcher,也会抛出InvalidOperationException - InvalidOperationException being thrown even though Dispatcher is being used 即使将数据传递回视图,foreach语句中的HTML也不会呈现 - HTML inside foreach statement not rendering even though data is being passed back to view 即使使用get / set方法,int值也不会随对象传递 - Int value not being passed with object even though get/set methods are used javascript函数未调用 - javascript function not being called 创建和重命名新文件夹后,FolderBrowserDialog 显示不正确的 SelectedPath - FolderBrowserDialog shows incorrect SelectedPath after new folder is being created and renamed 局部视图计数= 0,值未发送到控制器 - Partial View Count=0, value not being sent to the controller IWebElement发送到函数后变为空 - IWebElement becomes null after being sent to a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM