【发布时间】:2018-10-24 09:05:24
【问题描述】:
我有一个可以从一个输入字段复制文本值的工作按钮。
我需要做两个,但是当我更改值时,第一个按钮开始从第一个输入字段复制值。这是我的代码:
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.js">
</script>
<script>
function CopyCode(){
var text = document.getElementById("input-text");
text.select();
document.execCommand("copy");
}
var text = document.getElementById("element-id");
text.select();
document.execCommand("copy");
</script>
<input type="text" placeholder="CODE" value="CODE" style="text-align:center;" id="input-text"/>
<script type="text/javascript">
$('input').keypress(function(e) {
e.preventDefault();
});
</script>
<button onclick="CopyCode()">COPY</button> <br/> <br/>
<!--- SECOND --->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.js">
</script>
<script>
function CopyCode(){
var text = document.getElementById("coupon2-text");
text.select();
document.execCommand("copy");
}
var text = document.getElementById("element-id");
text.select();
document.execCommand("copy");
</script>
<input type="text" placeholder="CODE2" value="CODE2" style="text-align:center;" id="coupon2-text"/>
<script type="text/javascript">
$('input').keypress(function(e) {
e.preventDefault();
});
</script>
<button onclick="CopyCode()">COPY</button> <br/> <br/>
Tryit 编辑器:https://www.w3schools.com/code/tryit.asp?filename=FWJF0IFLP9D8 有什么建议吗?
【问题讨论】:
标签: javascript button input onclick copy