Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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
如何使用jquery操作querystring_Jquery_Url_Query String - Fatal编程技术网

如何使用jquery操作querystring

如何使用jquery操作querystring,jquery,url,query-string,Jquery,Url,Query String,我有一个选择下拉列表,其中id映射到值。 在onChange事件中,我想重定向到相同的url,但在querystring后面附加了'id=value' 如何检查querystring中是否没有此“id”选项(我不需要多个值),并根据需要替换/追加 如何检查querystring中是否已经有“?”并在没有“?”的情况下附加到url jquery有没有一种简单的方法可以做到这一点 对于$().ajax(url,{options})调用,它必须在后台执行类似的操作。 我希望我可以做$().redire

我有一个选择下拉列表,其中id映射到值。 在onChange事件中,我想重定向到相同的url,但在querystring后面附加了'id=value'

如何检查querystring中是否没有此“id”选项(我不需要多个值),并根据需要替换/追加

如何检查querystring中是否已经有“?”并在没有“?”的情况下附加到url

jquery有没有一种简单的方法可以做到这一点

对于
$().ajax(url,{options})
调用,它必须在后台执行类似的操作。 我希望我可以做
$().redirect(url,{id:$(this.val()})
之类的事情

提前谢谢

注意:此页面可能传递或不传递几个不同的查询选项(在其他表单选项上设置默认值),因此替换整个查询字符串不是一个选项

<html>
<head><script type="text/javascript" src="/jquery-1.3.2.min.js"></script></head>
<body>
  <script>
    $(function(){                                                                                                                                         
        $('#selector').change(function(){                                                                                                                 
            // problem if 'id' option already exists
            var url = location.href + '?;id=' + $(this).val();                                                                                            
            location.assign( url );
        });                                                                                                                                               
    });                                                                                                                                                   
  </script>

  <a href="#" onclick="location.assign( location.pathname )">reset</a>                                                                                      
  <form>                                                                                                                                                    
    <select id='selector' name='id'>                                                                                                                      
        <option value='1'>one</option>                                                                                                                    
        <option value='2'>two</option>                                                                                                                    
        <option value='3'>three</option>                                                                                                                  
    </select>                                                                                                                                             
  </form>
</body>
</html>

$(函数(){
$('#选择器')。更改(函数(){
//如果“id”选项已存在,则出现问题
var url=location.href+'?;id='+$(this.val();
地址分配(url);
});                                                                                                                                               
});                                                                                                                                                   
1
2
3

尝试使用拆分功能:

var url = location.href.split('?')[0] + '?;id=' + $(this).val();

split
返回由字符串(如“?”)拆分的字符串组成的列表,并删除该字符串。

这就像一个符咒:

jQuery.redirect = function( url ) {
   window.location.replace( url );  
};
$( "#selector" ).change( function() {
    var href = window.location.href.substring( 0, window.location.href.indexOf( '?' ) );
    $.redirect( href + '?id=' + $( this ).val() );
} );

设置
window.location.search
将更新url的查询字符串(如果已经存在,则覆盖该值)


最后我使用了一个RegExp匹配来覆盖选项。 param接受一个散列并将其序列化为查询字符串 但是它没有提供相反的结果(分解查询字符串)

然后在html中

$('#selector').change(function(){ 
   $.redirect( location.href, { id : $(this).val() })
})

感谢所有回应的人。

我怀疑所谓的解决方案不适用于特定的变量名。特别是线路:

RegExp( ';?' + key + '=?[^&;]*', 'g' )
如果key是querystring中其他地方出现的字符序列,则该出现的字符也会被错误地替换(请尝试使用一个字母,例如“a”)。这是因为正则表达式使字符串边界保持打开状态(前导“;”和尾随“=”都是可选的)。也许EpxPression最好是这样:

RegExp( '[;&]' + key + '=?[^&;]*', 'g' )

尽管我还没有测试过它。

根据CoffeeMonster的回答:

location.href包含哈希部分时出现问题:

www.example.com#hash
变为
www.example.com#hash?id=whatever
,这导致服务器无法解释
?id=whatever

通过删除url的哈希部分修复了此问题:
url.split(“#”)[0]

// put function into jQuery namespace
jQuery.redirect = function(url, params) {

    url = url || window.location.href || '';
    url = url.split("#")[0];
    url =  url.match(/\?/) ? url : url + '?';

    for ( var key in params ) {
        var re = RegExp( ';?' + key + '=?[^&;]*', 'g' );
        url = url.replace( re, '');
        url += ';' + key + '=' + params[key]; 
    }  
    // cleanup url 
    url = url.replace(/[;&]$/, '');
    url = url.replace(/\?[;&]/, '?'); 
    url = url.replace(/[;&]{2}/g, ';');
    // $(location).attr('href', url);
    window.location.replace( url ); 
};

现在
www.example.com#hash
变成
www.example.com?id=which

很好的bug发现!刚刚检查了我的代码,我正在传入
client\u id=12;项目id=45等等。但我知道只要使用
id=
就可以很容易地中断它。我想我会使用类似于
RegExp('[;&]?\b'…)
的东西在我开始工作时进行测试。谢谢,什么浏览器?你的HTML和JS是什么样子的?对不起,我把
.search
误认为是“search”querystring参数。实际上
search
表示DOM位置对象中的整个查询字符串。现在,为了获得额外的分数,请将哈希重新附加到url的末尾。:-)可能重复的
RegExp( '[;&]' + key + '=?[^&;]*', 'g' )
// put function into jQuery namespace
jQuery.redirect = function(url, params) {

    url = url || window.location.href || '';
    url = url.split("#")[0];
    url =  url.match(/\?/) ? url : url + '?';

    for ( var key in params ) {
        var re = RegExp( ';?' + key + '=?[^&;]*', 'g' );
        url = url.replace( re, '');
        url += ';' + key + '=' + params[key]; 
    }  
    // cleanup url 
    url = url.replace(/[;&]$/, '');
    url = url.replace(/\?[;&]/, '?'); 
    url = url.replace(/[;&]{2}/g, ';');
    // $(location).attr('href', url);
    window.location.replace( url ); 
};