在 jquery onclick 函数中,我想分配给一个 api,但首先我必须执行地址与某些字段值的串联,如下例所示。
问题是我没有正确执行串联。
此连接不正确。
var url = "https://api.whatsapp.com/send?phone=55"+ document.getElementById('Phone').value +" &text=Hellow,%20" + document.getElementById('Copy').value;
JQuery函数
$("#fidelizar").click(function () {
var url = "https://api.whatsapp.com/send?phone=55"+ document.getElementById('Phone').value +" &text=Hellow,%20" + document.getElementById('Copy').value;
window.open(url, '_blank');
});
最佳答案
尝试这样的事情 var url =
https://api.whatsapp.com/send?phone=55 ${$('#Phone').val()} &text=Hellow,%20${$('#Copy').val()};
.是string interpolation在这里您可以在普通字符串中嵌入表达式。
$("#fidelizar").click(function () {
var url = `https://api.whatsapp.com/send?phone=55${$('#Phone').val()}&text=Hellow,%20${$('#Copy').val()}`;
window.open(url, '_blank');
console.log(url);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="Phone" type="text">
<br/>
<input id="Copy" type="text">
<br/>
<button id="fidelizar">Click Me</button>
关于javascript - 如何在字符串 url 中间连接字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52088829/