Javascript 生成和复制用户生成的链接
我正在制作一个内部页面,以生成员工发送给客户的付款链接。工作人员应能够输入一个小数点,如Javascript 生成和复制用户生成的链接,javascript,php,html,Javascript,Php,Html,我正在制作一个内部页面,以生成员工发送给客户的付款链接。工作人员应能够输入一个小数点,如25.99,并与https://example.com/pay/给出https://example.com/pay/25.99 函数过程(){ var copyText=”https://example.com/pay/“+document.getElementById(“paylink”).value; copyText.select(); 文件。执行命令(“副本”); 警报(“复制链接:+copyTe
25.99
,并与https://example.com/pay/
给出https://example.com/pay/25.99
函数过程(){
var copyText=”https://example.com/pay/“+document.getElementById(“paylink”).value;
copyText.select();
文件。执行命令(“副本”);
警报(“复制链接:+copyText.value”);
}
以小数形式输入金额:
建议使用迪安·泰勒的答案
JavaScript
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
function process() {
var copyText ="https://example.com/pay/" + document.getElementById("paylink").value;
copyTextToClipboard(copyText);
}
jQuery
function copyTextToClipboard(text) {
var textArea = $("<textarea">).css({
position: "fixed",
top: 0,
left: 0,
width: "2em",
height: "2em",
padding: 0,
border: "none",
outline: "none",
"box-shadow": "none",
background: "transparent",
value: text
}).appendTo($("body"));
textArea[0].focus();
textArea[0].select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
textArea.remove();
}
function process() {
var copyText ="https://example.com/pay/" + $("#paylink").val();
copyTextToClipboard(copyText);
}
功能copyTextToClipboard(文本){
var textArea=$(“下面是您在JSFIDLE中修改的代码,操作输入框,您有一点时间来执行副本。(不确定何时启动实际的后端调用,因此必须基于此进行调整)
以小数形式输入金额:
函数过程(){
const amt=document.getElementById(“url”).value
var copyText=”https://example.com/pay/“+金额
document.getElementById(“url”).value=copyText
url.select();
文件。执行命令(“副本”);
document.getElementById(“url”).value=amt
}
它的可能副本不是,我知道怎么做。复制很好。不过它与元素ID@Jeto结合在一起,谢谢。Wlel您正在对字符串调用select()
,所以这不太好:)那么你的问题是什么?你想把URL复制到哪里?Greg Lowe对副本的回答应该为你工作,他的函数采用一个简单的文本字符串(例如,你的代码>拷贝文本< /代码>)作为输入。这就好多了。谢谢。我正处于切换到PHP的中间,然后复制它。
<form onClick="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>
<script>
function process() {
const amt = document.getElementById("url").value
var copyText ="https://example.com/pay/"+amt
document.getElementById("url").value=copyText
url.select();
document.execCommand("copy");
document.getElementById("url").value=amt
}
</script>