更改URL中的参数值

发布于 2024-12-02 23:50:53 字数 2082 浏览 0 评论 0原文

我试图创建一个 javascript 函数来使用文本框中输入的值来更改 URL 中参数的值,但没有成功。那是因为我不是一名代码设计师,而是一名图表设计师。 这是我需要更改“城市”参数的 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>

如何使其更改所选值上的参数值? 请各位设计师再次指教。 谢谢你!

L.E.

我做了一个解决方法,用这个改变了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('&'); 
    }

}

在StackOverflow上找到并使用$.result()函数从jQuery查询中调用它:

<script type="text/javascript">
$().ready(function() {
$("#city").autocomplete("core/exp_city.php", {
    width: 340,
    matchContains: true,
    selectFirst: false
}).result(function() {changeURI("city",this.value)});
});

</script>

I'm trying to make a javascript function to change the value of a parameter in the URL with the value inputed in a text box, with no luck. That's because I'm note a code designer but a graph one.
this is the URL where I need to change the "City" parameter:

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

I am generating data in the input text box through a MySQL query with jQuery like this:

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

How can I make it change the value the parameter on selected value?
Please advise, again, a humble designer.
Thank you!

L.E.

I have made an workaround, changed the changeURI() function with this one:

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('&'); 
    }

}

Found on StackOverflow and call it from the jQuery query with the $.result() function:

<script type="text/javascript">
$().ready(function() {
$("#city").autocomplete("core/exp_city.php", {
    width: 340,
    matchContains: true,
    selectFirst: false
}).result(function() {changeURI("city",this.value)});
});

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-12-09 23:50:53

你遇到什么错误?你收到任何 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

检查这是否是问题所在。

What error are you getting? are you getting any javascript error? Also, try changing your code to some thing like

url = url.replace(new RegExp("city=", 'g'), "city="+value).

Also, The URL written in the question should not have & before city parameter as the first parameter starts with a ?, so the URL should be :

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

Check if that was the issue.

欢烬 2024-12-09 23:50:53

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

In your example, document.location.search.substring(1) is already getting rid of the question mark: it should return &city=&uri=http://server/adauga_exp.php. Then doing a split on "?" and trying to take the second array element should return undefined, because there are no longer any "?" characters. Skip straight to var vars = query.split("&") at that point, and the rest looks okay to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文