【问题标题】:Change the url and reload on change of dropdown value which is in common header更改 url 并在更改公共标题中的下拉值时重新加载
【发布时间】:2014-02-06 07:01:55
【问题描述】:

我是 yii 的新手,我想更改 url 并在更改管理员公共标题字段中的下拉值时重新加载当前视图页面。 下拉菜单是

echo $form->dropDownList($siteid, 'selectedsiteid', $data,$htmlOptions, array('class' => "form-control"));

当前的url可能如下所示

1. http://localhost/zyntegra_commoditylifev2/admin/sitetag/viewTags/sid/6
2. http://localhost/zyntegra_commoditylifev2/admin/category/viewCategory/sid/ 3.http://localhost/zyntegra_commoditylifev2/admin/site/index


假设当前更改的下拉值是10,如果当前 url 像 (1) 之一,那么我需要像这样更改 url

@987654324 @

如果当前 url 类似于 (2) 则

http://localhost/zyntegra_commoditylifev2/admin/category/viewCategory/sid/10

如果它类似于 (3) 则无需重新加载。

请有人帮我解决这个问题。

提前致谢

【问题讨论】:

  • 你需要通过php吗?还是 javascript 就可以了?
  • javascript 没问题。我认为它不能只通过使用 php

标签: javascript php jquery yii yii-components


【解决方案1】:

试试看,

var tagsUrl='http://localhost/zyntegra_commoditylifev2/admin/sitetag/viewTags/sid/';
var catUrl='http://localhost/zyntegra_commoditylifev2/admin/category/viewCategory/sid/';
$(function(){
    $('#your-select-id').on('change',function(){
       href=window.location.href;
       if(href.indexOf('viewTags') !== -1){
           window.location.href=tagsUrl+this.value;//append value of select element
       } else if(href.indexOf('viewCategory') !== -1){
           window.location.href=catUrl+this.value;//append value of select element
       }
    });
});

以上代码可以帮助您完成任务。

已更新,只获取 sid 然后试试看,

// use location.href in url to get current url
var url='http://localhost/zyntegra_commoditylifev2/admin/sitetag/viewTags/sid/110';
alert(url.split('sid/')[1]);//110 

更新中 @salini 回答喜欢,

function reloadpage() {             
    var sid = $( '#Site_selectedsiteid' ).val( );
    var currentURL = window.location.pathname;
    var n=currentURL.indexOf("/sid");// use '/sid' only in indexOf to remove else part
    if(n!=-1) {
        var newurl = currentURL.substring(0,n); 
        window.location.href = newurl + '/sid/' + sid;
    }
}

【讨论】:

  • 但是像这样的网址还有很多,我只提到了其中的一些
  • @Salini 您是否尝试过上述解决方案?您面临的问题是什么?
  • @kumar 还有更多类似的网址 localhost/zyntegra_commoditylifev2/admin/sitetag/tags/sid
  • @Salini 但是,网址相同但参数不同。对吗?
  • 如果 url 的最后一部分像 /sid/ 或 /sid/2(某个值),我需要将其更改为 /sid/10(当前下拉值)
【解决方案2】:

是的,我已经解决了。这是我的 iavascript 代码

function reloadpage() {
     var sid = $('#Site_selectedsiteid').val();
     var currentURL = window.location.pathname;
     var n = currentURL.indexOf("/sid/");
     if (n != -1) {
         var newurl = currentURL.substring(0, n);
         window.location.href = newurl + '/sid/' + sid;
     } else {
         var n = currentURL.indexOf("/sid");
         if (n != -1) {
             var newurl = currentURL.substring(0, n);
             window.location.href = newurl + '/sid/' + sid;
         }
     }
}


它工作正常。感谢@Roshan 和@kumar 的帮助

【讨论】:

  • 我在查看您的答案后再次更新了我的答案尝试一次。
猜你喜欢
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 2014-01-10
  • 2021-10-24
  • 1970-01-01
相关资源
最近更新 更多