首页 > 解决方案 > 根据复制到剪贴板的用户输入生成链接

问题描述

我正在为员工创建一个表格,用于生成链接。我们的网站处理付款。这个想法是工作人员可以输入 23 美元作为 23.00,它将带用户到example.com/pay/23.00

当前按下“获取链接”按钮会将用户带到付款地址。

<script>
function process()
{
var url="http://example.com/pay/" + document.getElementById("url").value;
location.href=url;
return false;
}
</script>
<script>
function myFunction() {
  var copyText = document.getElementById("url");
  copyText.select();
  document.execCommand("copy");
  alert("Copied the URL: " + copyText.value);
}
</script>
<form onSubmit="return process();">
<p>Enter the amount as a decimal:</p><br>
<input type="text" name="url" id="url"> <input type="submit" value="Get Link" id="paylink">
</form>

get link应该复制 URL 而不是重定向。

任何帮助将不胜感激,因为我快到了。

标签: javascripthtmlforms

解决方案


<script>
function process()
{
   var url="http://example.com/pay/" + document.getElementById("url").value;
   document.getElementById("url").value = url;
   var copyText = document.getElementById("url");
   copyText.select();
   document.execCommand("copy");
   alert("Copied the URL: " + copyText.value);
   return false;
}
</script>
<p>Enter the amount as a decimal:</p><br>
<input type="text" name="url" id="url"> <input type="button" onclick="process()" value="Get Link" id="paylink">

此外,您应该进行一些验证。


推荐阅读