javascript - 如何在字符串 url 中间连接字段

标签 javascript jquery

在 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/

相关文章:

javascript - 我需要用 JavaScript 在后台打开一个新窗口,并确保原始窗口仍然处于焦点状态

javascript - 在遍历两个数组时查找共同值

javascript - TweenMax 悬停未触发动画

javascript - 如何对 Bootstrap 卡进行分页

javascript - 复制时的 JQuery DatePicker 日期格式问题

javascript - jQuery 动画到视口(viewport)顶部

javascript - JQuery SVG 和字体着色

javascript - 保存条目后,nodejs mongoose 不会返回控制台

javascript - 返回 Promise 的函数的嵌套数组

javascript - {()=> (this.whatever)} 与 {this.whatever}