简体   繁体   English

Ajax / Javascript删除无对话框的确认文本

[英]Ajax/Javascript Delete Confirm Text Without Dialog

I have searched everywhere but cannot seem to find any examples anywhere of what I am trying to achieve. 我到处搜索过,但似乎找不到我想要实现的任何示例。

I have a Delete button, and when you press the button I would like the text to appear on the screen without the page refreshing asking the user with Yes/No hyperlinks (not buttons) if they want to delete their message. 我有一个“删除”按钮,当您按下该按钮时,我希望文本出现在屏幕上而无需刷新页面,询问用户“是/否”超链接(不是按钮)是否要删除其消息。 I strictly do not want to use a modal or dialog, I just want text on screen. 我严格不希望使用模式对话框,而只希望在屏幕上显示文本。

I have tried the following script on the ajax page but doesnt do exactly what I want: 我已经在ajax页面上尝试了以下脚本,但是并没有完全按照我的意愿进行:

 function loadDoc() { var xhttp; if (window.XMLHttpRequest) { // code for modern browsers xhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { document.getElementById("demo").innerHTML = xhttp.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); } 
 <p id="demo">Let AJAX change this text.</p> <button type='submit' name='btn-delete' id='btn-delete' onclick='loadDoc()'>Delete</button> 

As my button is set to submit, I know its posting to the next page before asking the user for delete confirmation. 由于我的按钮已设置为提交,因此在要求用户确认删除之前,我知道它已发布到下一页。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks in advance. 提前致谢。

WORKING JSFIDDLE 工作的JSFIDDLE

You essentially need to first wrap your markup in a container <div> to append your confirmation <a> tags when they are created. 本质上,您首先需要将标记包装在容器<div>以在创建确认<a>标记时附加它们。

<div id="container">
  <p id="demo">Let AJAX change this text.</p>
</div>

Then create the confirmation <a> tags and append them to the container <div> . 然后创建确认<a>标记并将其附加到容器<div>

var node = document.createElement("a");
var textnode = document.createTextNode("Yes");
node.appendChild(textnode);
node.id = 'confirm-delete';
node.href = 'javascript:void(0)';
document.getElementById("container").appendChild(node);

Then fire the request when the user clicks the newly created <a> tag. 然后,当用户单击新创建的<a>标记时,触发请求。

document.getElementById('confirm-delete').onclick = function confirmation() {
  // ...
}

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

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