简体   繁体   English

如何创建到相应表的按钮,该表是服务器的响应,并单击按钮将内容作为发布请求发送到同一服务器?

[英]How to create a button to respective table which is a response from a server and on button click send the contents as post request to the same server?

I have a javascript code which sends a GET request to the server and receives the XML response and puts the contents into the table,I'm supposed to create buttons to each row and when its clicked it should post the row contents back to the server,how could I do this? 我有一个javascript代码,它将GET请求发送到服务器,并接收XML响应,然后将内容放入表中。我应该为每行创建一个按钮,当单击它时,应将行内容发回到服务器上,我该怎么做?

 function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "http://10.114.56.217:8080/Online_shopping_cart/services/smartphonelist", true); xhttp.send(); } function myFunction(xml) { var i; var xmlDoc = xml.responseXML; var table="<tr><th>TYPE</th><th>MAKE</th><th>PRICE</th><th>MODEL</th><th>ID</th><th>Select</th></tr>"; var x = xmlDoc.getElementsByTagName("itemnumber"); for (i = 0; i <x.length; i++) { table += "<tr><td>" + x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("make")[0].childNodes[0].nodeValue + "</td><td>"+ x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue + "</td><td>" + x[i].getElementsByTagName("model")[0].childNodes[0].nodeValue + "</td><td>"+ x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue + "</td></tr>"; } document.getElementById("dem").innerHTML = table; } 
 <html> <button type="button" onclick="loadDoc()">Smartphones</button> <table id="dem"></table> </html> 
My response snapshot is shown in the below link 我的回复快照显示在下面的链接中

https://i.stack.imgur.com/iyzCL.png https://i.stack.imgur.com/iyzCL.png

I am not much experienced with javascript, as of your question need to add button in each row with different identifier, so on brief there are various way of doing it and one of the way is below, 我对javascript没有太多经验,因为您的问题需要在每行中添加具有不同标识符的按钮,因此简要来说,有多种方法可以使用,其中一种方法如下,

  1. When you insert contents in table u can there itself create a button for each row same like html but you can add suffix of incremental number to button name. 当您在表u中插入内容时,它本身可以为每行创建一个按钮,例如html,但您可以在按钮名称后添加增量编号的后缀。 (like : name0, name1) as directly concatenate i with button name. (例如:name0,name1)直接与按钮名称连接在一起。

  2. on click on every function call same function with that button name, and in function extract suffix number from button name. 在每个函数上单击时,都使用该按钮名称调用相同的函数,然后在函数中从按钮名称中提取后缀号。

  3. now with that number you can find the what need to be send in next request. 现在使用该号码,您可以找到下一个请求中需要发送的内容。

This not might be perfect solution but yes it will help you. 这可能不是完美的解决方案,但是可以,它将为您提供帮助。

Thank You 谢谢

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

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