简体   繁体   English

InnerHTML不会为Servlet带来价值

[英]InnerHTML doesn't brings the value to servlet

cell = this.getElementsByTagName("td")[3];
uname = cell.innerHTML;

i get the value of the particular cell through innerHTML and pass that value to Servlet 我通过innerHTML获取特定单元格的值,并将该值传递给Servlet

xmlhttp.open("POST","UserServlet",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("uname="+uname);

in Servlet 在Servlet中

uname = request.getParameter("uname");
        out.print(uname);
        System.out.println(uname);

i get "undefined" in console....... is there any way get the value and pass that to servlet i tried .innerHTML,.innerText,.value nothing worked,i tried in array too....but nothing worked......Help me thanks in advance..... 我在控制台中得到“未定义” .......有什么办法将值传递给servlet我尝试了.innerHTML,.innerText,.value没用,我也在数组中尝试了....但是没有工作......提前帮助我.....

Please refer to https://stackoverflow.com/a/15312976/1031191 . 请参阅https://stackoverflow.com/a/15312976/1031191 That implies that your xmlhttp code is just fine. 这意味着您的xmlhttp代码就可以了。 Try and use javascript console in the browser to verify that uname is a string and contains the right data. 尝试在浏览器中使用JavaScript控制台来验证uname是否为字符串并包含正确的数据。

Receiving "undefined" means that the value of uname is exactly "undefined" on the client side. 接收到“ undefined”意味着在客户端, uname的值恰好是“ undefined”。 See getParameter's reference: http://docs.oracle.com/javaee/1.3/api/javax/servlet/ServletRequest.html . 请参见getParameter的参考: http : //docs.oracle.com/javaee/1.3/api/javax/servlet/ServletRequest.html It says that you must receive either a String or null. 它说您必须收到一个String或null。 (so in your case the argument of xmlhttp.send() is "uname=undefined" for some reason.) (因此,出于某种原因,xmlhttp.send()的参数为“ uname = undefined”。)

UPDATE 2: 更新2:

Probably you need document.getElementsByTagName('td')[3] instead of "this". 可能您需要document.getElementsByTagName('td')[3]代替“ this”。
But hey, if you use jQuery anyway, why don't you write $('td').get(3) instead of getElementsByTagName? 但是,嘿,如果仍然使用jQuery,为什么不编写$('td').get(3)而不是getElementsByTagName?

UPDATE 3: 更新3:

I think you have less than 4 td elements in your html. 我认为你必须在你的HTML小于4个td元素。 Please note that javascript arrays are indexed from 0. You receive "undefined" if you accidentally try to access an index in an array that is out of bounds. 请注意,javascript数组的索引从0开始。如果您不小心尝试访问超出范围的数组中的索引,则会收到“ undefined”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM