【问题标题】:How to change the url according to the combobox value in mvc3?如何根据mvc3中的combobox值更改url?
【发布时间】:2011-11-17 02:29:12
【问题描述】:

当我更改页面路径时,如下所示: localhost/XXX/fr - 页面是法语的 当我写: localhost/XXX/en - 页面是英文的。 (文本取自资源文件)。

我还有一个组合框,其中包含 value=en 的“english” 和 value="fr" 的 "french"。

现在,如何通过所选值更改 URL? 我在想也许我应该写一些类似的东西-

controllerName/.../theSelectedValue

但我不知道该怎么做。

(在JQuery中选择的值是=$("#combobox")[0].value,可以用JQuery写吗?)


我确实这样做了:

var urlString = window.location.host;   //the url with localhost:XXX only -and if it changes it will adjust itself 

var Lang = $(this)[0].value;            //en or fr
window.location = "http://" + urlString + "/" + Lang;

但现在我遇到了另一个问题:window.location - 刷新页面,并且 $(this)[0].value - 返回到开头。例如,如果我将组合框从“英语”更改为“法语”,语言确实会更改为法语(因为 url 未刷新),但在组合框中,所选字段再次为“英语”,我无法更改它,因为所有页面被刷新。 所以...有人可以告诉我该怎么做吗??

提前致谢。

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-3 combobox routing


    【解决方案1】:

    您可以附加到组合框的.change 事件,如下所示:

    $('#CLIENT_ID_FOR_YOUR_CHECKBOX_HERE').change(function() {
      window.location = "http://localhost/XXX/" + $(this).val();
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 2017-02-05
      • 1970-01-01
      • 2011-11-08
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多