使用Jquery从动态链接中取出url信息 - 下拉列表

时间:2012-02-22 19:24:31

标签: jquery url

美好的一天,我有两个下降。一个加载第二个数据。用户在第二个下拉列表中选择一个选项后,页面会自动转发到该链接。

我正在尝试从下拉列表指向的网址中删除一些元素:

  

http://local.mysite/someplace/county/north-hempstead-ny/orange/gardnertown

我想要

  

http://local.mysite/orange/gardnertown

我已经发现了 window.location.href.split('/')[2]给了我想要的东西。然而,下拉选择部分让我失望。

这是需要修复的部分我认为:

window.location.href.split('/')[2] + $places
      .append("<option value=" + this.url + ">" + this.community + "</option>");

这是 fiddle


感谢您的回复。我试图添加代码,但它仍然没有删除额外的URL文本。控制台也没有显示剥离。这是我修改后的代码:

   document.getElementById('page-changer').reset();
$("#county").change(function() {
    var $places = $("#township").empty();
    $.each(data[$(this).val() - 1], function() {
        $places.append("<option value=" + this.url + ">" + this.community + "</option>");
     $('#jumpSubmit').click(function(e) {
      e.preventDefault();
       window.location = window.location.href.split('/')[2] + '/' + $('#township').val();
      //console.log(window.location.href.split('/')[2]) + '/' + $('#township').val());

2 个答案:

答案 0 :(得分:0)

您只需要<select>

中的值
var $full_url = $("#your_select").val();
var $part_you_need = $full_url.split('/')[2];
alert( $part_you_need );

答案 1 :(得分:0)

您可以使用jQuery根据您的select元素构建您喜欢的URL。例如:

$('#jumpSubmit').click(function(e) {
    e.preventDefault();
    window.location = window.location.href.split('/')[2] + '/' + $('#township').val();
    //console.log(window.location.href.split('/')[2] + '/' + $('#township').val());
});​
​

更新了使用现有网址与硬编码网址的代码。