看看下面的代码是不是你想要的效果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档 </title>
</head>
<script type="text/javascript">
function setParam(key, value) {
var url = window.location;
var exp = new RegExp('^(.*)(\\\?|&)' + key + '=([^&]*)(.*)$');
var result = exp.exec(url);
if (result == null) {
window.location+= (window.location.search ? "&" : "?") + key + "=" + value;
} else {
window.location = result[1]+result[2]+key+"="+value+result[4];
}
}
</script>
<body><input type="text" id="key"/>
<input type="button" onclick="setParam('key', document.getElementById('key').value);" value="submit"/></body>
</html> 

解决方案 »

  1.   

    <script language="javascript" type="text/javascript">
                                        function changeparam(searchname, searchv)
                                        {
                                            var href=window.location.href;//网址全部地址
                                            var vyes=window.location.search;//取地址栏所有参数,为空没有参数                                        if(vyes!='')//地址栏是否有参数
                                            {//地址栏有参数
                                                var r;
                                                r=vyes.search(searchname);//参数出现的位置                                            if(r!=-1)//是否有这个参数
                                                {//如果查询中有这个参数
    var rtop,rend;
                                                    rtop=vyes.substr(0,r);//这个参数前面的参数
                                                    rend=vyes.substr(r);//这个参数及其后面的参数
                                                    var r2=rend.search('&');//后面参数连接符的位置
                                                    //var r2=searchname.length+searchv.length+1;                                                if(r2!=-1)//这个参数后面是否有参数
                                                    {//这个参数后面有参数
                                                        rend=rend.substr(r2);//这个参数后面的参数
        //alert(rtop+searchname+'='+searchv+rend);//新的参数
        //alert(window.location.pathname);//不带参数的地址
                                                        location=window.location.pathname+rtop+searchname+'='+searchv+rend;
                                                        //return window.location.pathname+rtop+searchname+'='+searchv+rend;
                                                    }
                                                    else
                                                    {//如果后面没有&了.
                                                        location=window.location.pathname+rtop+searchname+'='+searchv;
                                                        //return window.location.pathname+rtop+searchname+'='+searchv;
                                                    }
                                                }
                                                else
                                                {//如果没有这个参数,加上参数及其对应的值
                                                    location=href+'&'+searchname+'='+searchv;
                                                    //return href+'&'+searchname+'='+searchv;
                                                }
                                            }
                                            else
                                            {//地址栏没参数,加上参数及其对应的值
                                                location=href+'?'+searchname+'='+searchv;
                                                //return href+'?'+searchname+'='+searchv;
                                            }
                                        }
                                        function hrefurl(searchname, searchv)
                                        {//alert(searchv);
                                            if(window.location.href != changeparam(searchname, searchv))
                                            {alert(changeparam(searchname, searchv));
                                                window.location == changeparam(searchname, searchv);
                                            }
                                        }
                                        </script>