如何使用Jquery检查URL参数值是否存在?

时间:2016-08-08 12:50:38

标签: javascript jquery html select

<select onchange="select_serviceid();" id="exist_service" name="exist_service" class="validate[required] form-control">
    <option value="">Select from existing Service Apartment</option>
    <option value="1">test123</option>
    <option value="2">Test Provab</option>
    <option value="3">Test Provab</option>
    <option value="4">Test Provab dated</option>
    <option value="5">Test Provab123456789</option>
</select>
<script>
   function select_serviceid(){
      var select1 = $( "#exist_service option:selected" ).val();
      var url = document.URL+'?service='+select1;
      $(location).attr('href', url);
   }
</script>

这里我将服务ID重定向到URL并重新加载它,但我想先检查当前URL中是否有参数。如果不是那么我添加参数,否则仅更改id值。

这是网址

http://192.168.0.24/corporate_rooms/CRS/property/service_basicinfo?service=3

参数服务希望在加载时更改

4 个答案:

答案 0 :(得分:1)

在选择功能

onchange="getServiceId(this);" # To pass the value use this parameter

在JQuery中

function getServiceId(sel) {
    var id = sel.value;
    if(id == '')
    {
        alert('empty');
    }
    else
    {
        # Your code here
    }
}

编辑01

Get current URL in web browser

var url = window.location.href+'?service='+id;

How to redirect to another page in jQuery?

// similar behavior as an HTTP redirect
window.location.replace(url);

// similar behavior as clicking on a link
window.location.href = url;

答案 1 :(得分:0)

if($( "#exist_service option:selected" ).val())
{

       //value present


        var select1 = $( "#exist_service option:selected" ).val();
        var url = document.URL+'?service='+select1;
        $(location).attr('href', url);

}else
{

        //value not present


}

答案 2 :(得分:0)

你可以像这样重写你的代码。

<select id="exist_service" name="exist_service" class="validate[required] form-control">
    <option value="">Select from existing Service Apartment</option>
    <option value="1">test123</option>
    <option value="2">Test Provab</option>
    <option value="3">Test Provab</option>
    <option value="4">Test Provab dated</option>
    <option value="5">Test Provab123456789</option>
</select>
<script>
$(document).ready(function(){
    $('#exist_service').change(function(){
        var select1 = $(this).val();
        var url = document.URL+'?service='+select1;
        $(location).attr('href', url);
    })
})
</script>

答案 3 :(得分:0)

您可以使用函数替换url中的参数

function updateQueryStringParameter(uri, key, value) {

uri=uri.replace("#","");
var re = new RegExp("([?|&])" + key + "=.*?(&|$)", "i");
separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
    return uri + separator + key + "=" + value;
}
}

使用示例

 var pathname = window.location.toString();
 pathname=updateQueryStringParameter(pathname,"datasource",b);
 window.location = updateQueryStringParameter(pathname,"setdata",a);

或者检查参数是否为:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
        vars[key] = value;
    });
    return vars;
}
//example usage
var first = getUrlVars()["setdata"];

    if (first != null&&typeof(first)!= 'undefined') {
      //do something

    }