如何更改URL参数格式

时间:2017-12-18 04:45:15

标签: javascript html angularjs

当我通过url传递参数时,我想改变它们的格式。

这可能吗?

http://localhost:51406/home/Booking?Rooms=1&Destination={  "name": "Mumbai, India",  "code": "BOM"}&DepartureDate=12/18/2017&ReturnDate=12/19/2017&Adults1=1&Children1=0&C1Age1=0&C1Age2=0&C1Age3=0&C1Age4=0&Adults2=0&Children2=0&C2Age1=0&C2Age2=0&C2Age3=0&C2Age4=0&Adults3=0&Children3=0&C3Age1=0&C3Age2=0&C3Age3=0&C3Age4=0

我想改变这个

http://***/home/Booking?Rooms=1&Destination=%22Mumbai,%20India(BOM)%22&DepartureDate=12/18/2017&ReturnDate=12/21/2017&Adults1=1&Children1=0&C1Age1=0&C1Age2=0&C1Age3=0&C1Age4=0&Adults2=0&Children2=0&C2Age1=0&C2Age2=0&C2Age3=0&C2Age4=0&Adults3=0&Children3=0&C3Age1=0&C3Age2=0&C3Age3=0&C3Age4=0

目的地应该像第二个一样改变

这是searchcontroller.js

app.factory("States", function () {
    var states = [ { 'name' : 'Ansan, South Korea', 'code': 'A1N' },
                   { 'name' : 'Asan, South Korea', 'code': 'A1S' },
                   { 'name' : 'Mumbai, India', 'code': 'BOM' },
                   { 'name' : 'Atascadero - CA, United States', 'code': 'AA1' },
                   { 'name' : 'London, United Kingdom', 'code': 'LON' },
                   { 'name' : 'Arlon, Belgium', 'code': 'AAO' } ] 
    return states; 
}); 

这是视图部分代码

   <input name="states" id="city"
          style="margin-left: 0px;margin-right: 109px; font-size: 14px" 
          type="text" placeholder="Any worldwide city or airport"
          ng-model="selectedNumberNonEditable" allow-custom="false"
          typeahead="state as state.name + ' (' + state.code + ')' for state in states | filter:$viewValue | limitTo:8"
          class="form-control" required>

从评论

添加
<a id="sbutton" target="_blank" class="btn btn-primary btn-lg"
   style="background-color: #E28F13; font-size: 14px"
   ng-href="@Url.Action("Booking", "home")?Rooms={{rooms}}
            &Destination={{selectedNumberNonEdita‌​ble|json}}
            &Departure‌​Date={{departuredate‌​}}"
   onclick="validate(event);"> Search </a>

1 个答案:

答案 0 :(得分:1)

Your need to use encodeURI

var param = 'Destination={  "name": "Mumbai, India",  "code": "BOM"}';

var encodedParam = encodeURI(param );
console.log(encodedParam );

Or

Are you looking something like this

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var url = 'http://localhost:51406/home/Booking?Rooms=1&Destination={  "name": "Mumbai, India",  "code": "BOM"}&DepartureDate=12/18/2017&ReturnDate=12/19/2017&Adults1=1&Children1=0&C1Age1=0&C1Age2=0&C1Age3=0&C1Age4=0&Adults2=0&Children2=0&C2Age1=0&C2Age2=0&C2Age3=0&C2Age4=0&Adults3=0&Children3=0&C3Age1=0&C3Age2=0&C3Age3=0&C3Age4=0';

var destination = JSON.parse(getParameterByName("Destination", url));
var newDestination = destination.name + "(" + destination.code + ")";

console.log(newDestination);