Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改URL中参数的值_Javascript_Jquery_Url_Parameters - Fatal编程技术网

Javascript 更改URL中参数的值

Javascript 更改URL中参数的值,javascript,jquery,url,parameters,Javascript,Jquery,Url,Parameters,我正试图创建一个javascript函数,用在文本框中输入的值来更改URL中参数的值,但运气不好。这是因为我是一名代码设计师,但我是一名图形设计师。 这是我需要更改“City”参数的URL: http://server/ad_new_customer.php?&city=&uri=http://server/adauga_exp.php 我使用jQuery通过MySQL查询在输入文本框中生成数据,如下所示: <input type='text' id='city' nam

我正试图创建一个javascript函数,用在文本框中输入的值来更改URL中参数的值,但运气不好。这是因为我是一名代码设计师,但我是一名图形设计师。 这是我需要更改“City”参数的URL:

http://server/ad_new_customer.php?&city=&uri=http://server/adauga_exp.php
我使用jQuery通过MySQL查询在输入文本框中生成数据,如下所示:

<input type='text' id='city' name='city' style="width:190px; align:left;" value="<?php echo $city; ?>" /> </td>
<script type="text/javascript">
//change the value of parameter in the URL function
function changeURI(key, value) {
var query = document.location.search.substring(1);
var query_q = query.split("?");
var vars = query_q[1].split("&");
for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == key) {
        vars[i] = pair[0] + "=" + value;
    }
}
return vars.join("&");
}
//jQuery making the auto-suggestion query for the input ID
$().ready(function() {
$("#city").autocomplete("core/exp_city.php", {
    width: 340,
    matchContains: true,
    selectFirst: false
}).return(changeURI("city", this.value}));
});
</script>
我已经做了一个变通方法,用以下方法更改了changeURI()函数:

function changeURI(key, value)
{
key = escape(key); value = escape(value);
var kvp = document.location.search.substr(1).split('&');
var i=kvp.length; var x; while(i--) 
{
    x = kvp[i].split('=');
    if (x[0]==key)
    {
            x[1] = value;
            kvp[i] = x.join('=');
            break;
    }
}
if(i<0) {
    kvp[kvp.length] = [key,value].join('=');
    }else{
//this will reload the page, it's likely better to store this until finished
document.location.search = kvp.join('&'); 
    }

}
函数changeURI(键、值)
{
键=转义(键);值=转义(值);
var kvp=document.location.search.substr(1).split('&');
var i=kvp.length;var x;while(i--)
{
x=kvp[i]。拆分('=');
如果(x[0]==键)
{
x[1]=数值;
kvp[i]=x.join('=');
打破
}
}

如果(i您得到了什么错误?您得到了任何javascript错误吗?另外,尝试将您的代码更改为

url = url.replace(new RegExp("city=", 'g'), "city="+value).
此外,问题中写入的URL不应在城市参数之前包含
&
,因为第一个参数以
开头,因此URL应为:

http://server/ad_new_customer.php?city=&uri=http://server/adauga_exp.php

检查这是否是问题所在。

在您的示例中,
document.location.search.substring(1)
已经去掉了问号:它应该返回
&city=&uri=http://server/adauga_exp.php
。然后在“?”上执行拆分并尝试获取第二个数组元素时应返回未定义,因为不再有任何数组元素“?”字符。此时直接跳到
var vars=query.split(&“
),其余的看起来没问题。

Hi@Ankit,实际上它没有给我任何错误,只是没有进行所需的更改,我修改了第一个参数,仅以“?”开头,但运气不好。我不知道如何实现替换(new RegExp())我读到它比其他解决方案慢。谢谢你。谢谢@sthobrien,你是对的,我跳过了query_q变量,但仍然不起作用。无论如何,我已经设法使用了另一个changeURI()函数,它确实起作用了。谢谢。
url = url.replace(new RegExp("city=", 'g'), "city="+value).
http://server/ad_new_customer.php?city=&uri=http://server/adauga_exp.php