替换url查询字符串值 [英] replace url query string value

查看:81
本文介绍了替换url查询字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用数据属性动态生成了 url 按钮

I have dynamically generated buttons with url using data attributes

<button class="btn-Add" type="button" data-add-url="/AddProductToCart?mobileNo=__param__&amp;
productId=2051&amp;quantity=1">Add to Cart</button>

我有以下代码:

$(document).ready(function () {
    var mobileNo = 0;
    $(".btn-Add").click(function () {
        mobileNo = $(this).parent('.pro-Group').find('input[type=text]').val();
        var postURL = $(this).data('add-url');
        postURL.replace('__param__', mobileNo);
        alert(postURL); // i can still see __param__ , its not replaced
    });
});

为什么在使用replace方法后不会替换查询字符串值?

Why query string value is not replaced after using replace method ?

推荐答案

Javascript字符串是不可变的。因此,一旦他们的价值被分配,他们就无法改变他们的价值

Javascript Strings are immutable. So once their value got assigned they can't changes their values.

因此,您必须使用新更改重新分配它们,或者必须在新变量中接收它们。通常我们会重新分配,因为除非您需要旧值,否则不需要引入新变量。

So you have to reassign them with new changes or have to receive them in new variables. Generally we reassign as there is no need of new variable to be introduced unless you need old value .

  postURL = postURL.replace('__param__', mobileNo);

这篇关于替换url查询字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆