DGT*_*DGT 1 javascript ajax jquery
当我<textarea></textarea>点击另一个html页面中的链接时,我试图在文件中插入一些文本到html文件中.
html1.html:
<a href="#" id="link">click to add text</a>
Run Code Online (Sandbox Code Playgroud)
html2.html:
<form action="...">
<textarea id="txt"></textarea>
</form>
Run Code Online (Sandbox Code Playgroud)
JS:
$(".link").click(function(){
window.location = "./html2.html";
var text = $("#some_id").text();
$("#txt").val(text)
});
Run Code Online (Sandbox Code Playgroud)
迁移到新页面后,任何进一步的JS执行都将丢失.你必须做这样的事情,将文本传递给查询字符串变量中的新页面:
$(".link").click(function(){
var encodedText = encodeURIComponent($("#some_id").text());
window.location = "./html2.html?txt=" + encodedText;
});
Run Code Online (Sandbox Code Playgroud)
并在您的html2.html页面上有这样的代码:
var capturedText = window.location.search.match(/(\?|&)txt=(.*?)(&|$)/);
capturedText = capturedText ? decodeURIComponent(capturedText[2]) : '';
$("#txt").val(capturedText);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
548 次 |
| 最近记录: |