且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在按钮单击时从文本框和下拉列表中复制数据

更新时间:2023-11-03 13:21:40

<html><head><script language="JavaScript">

function ClipBoard()
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}

</script>
</head>
<body>

<span id="copytext" style="height:150;width:162;background-color:pink">
This text will be copied onto the clipboard when you click the button below. Try it!
</span>

<textarea id="holdtext" style="display:none;">
</textarea>
<button onclick="ClipBoard();">Copy to Clipboard</button> 

</body>
</html>





上面的代码会将跨度的值复制到剪贴板。



但正如您所提到的。您必须复制所有控件的文本,您可以遍历所有输入控件,找到每个控件的值/文本,将其存储在某些控件中字典格式,例如< controlid,value> 。然后在元素焦点上,您可以从字典中找到特定控件的值,将文本设置为剪贴板文本,当用户按下粘贴时,之前的数据将粘贴在输入字段中



希望有所帮助。



[快乐编码]



The above code will copy the value of a span to clipboard .

But as you have mentioned .You have to copy the text of all the controls, you can iterate through all the input controls , find the value / text of each control , store it in some dictionary format , like <controlid,value> . Then on focus of element you can find the value of a specific control from the dictionary , set the text as clipboard text , and when user will press paste , the previous data will be pasted in the input field

Hope that helps.

[ Happy Coding ]