Jquery:要在下拉列表中从一个页面切换到另一个页面,请单击

Jquery:要在下拉列表中从一个页面切换到另一个页面,请单击,jquery,html,Jquery,Html,我的页面中有一个下拉列表,其中包含两个选项。我想要的是,当用户在下拉列表中选择第二个值时,应该加载另一个页面。这意味着如何在单击下拉框值时切换页面。 为了寻求帮助,我把我的html代码的下拉列表 <select id = "viewList" class="fl width160"> <option>Target</option> <option>Source</option&g

我的页面中有一个下拉列表,其中包含两个选项。我想要的是,当用户在下拉列表中选择第二个值时,应该加载另一个页面。这意味着如何在单击下拉框值时切换页面。 为了寻求帮助,我把我的html代码的下拉列表

        <select id = "viewList" class="fl width160">
            <option>Target</option>
            <option>Source</option>
        </select>
所以现在当用户点击源代码选项时,另一个js页面应该会打开。我应该如何在.js fileJquery中为其编写代码。

以下是代码:

$("#viewList").change(function() {
   if ($(this).find(':selected').text() == 'Source')
      window.location = 'urltogoto';
});
  <select id ="viewList" class="fl width160">
      <option value="" selected>Please select</option>
      <option value="http://www.google.com">Google</option>
      <option value="http://www.yahoo.com">Yahoo</option>
  </select>
<script type="text/javascript">
$(function(){$('#viewList').bind('change', function(){if($(this).val()) window.location=$(this).val();});});
</script>​
看。请注意,它可能无法在JSFIDLE中工作,因为它在iframe中运行代码。您可以测试它

这是代码:

  <select id ="viewList" class="fl width160">
      <option value="" selected>Please select</option>
      <option value="http://www.google.com">Google</option>
      <option value="http://www.yahoo.com">Yahoo</option>
  </select>
<script type="text/javascript">
$(function(){$('#viewList').bind('change', function(){if($(this).val()) window.location=$(this).val();});});
</script>​

看。请注意,它可能无法在JSFIDLE中工作,因为它在iframe中运行代码。您可以测试它。

来自@PenchoIlchev的解决方案是有效的,但如果您不想使用bind方法,您可以尝试:

 <select id = "viewList" class="fl width160" onchange="change();">
        <option>Target</option>
        <option>Source</option>
    </select>​

function change(){
    url = $("#viewList option:selected").html();
    location.href = url;
}​

@PenchoIlchev的解决方案是有效的,但如果您不想使用bind方法,您可以尝试:

 <select id = "viewList" class="fl width160" onchange="change();">
        <option>Target</option>
        <option>Source</option>
    </select>​

function change(){
    url = $("#viewList option:selected").html();
    location.href = url;
}​

你需要像这样修改你的html代码

 <select id = "viewList" class="fl width160">
        <option value ="Target">Target</option>
        <option value ="Source">Source</option>
    </select>

$("#viewList").change(function() {
        if( $("#viewList").val =='Target')
    {
        _loadTargetList();
    }
    else
    {
        _loadSourceList();
    }   
});

希望这有帮助。

看,您需要像这样更改html代码

 <select id = "viewList" class="fl width160">
        <option value ="Target">Target</option>
        <option value ="Source">Source</option>
    </select>

$("#viewList").change(function() {
        if( $("#viewList").val =='Target')
    {
        _loadTargetList();
    }
    else
    {
        _loadSourceList();
    }   
});

希望这有帮助。

我正在寻找这样的答案,但还是没有得到。如果我的源文件位于c:/etc/source位置,那么我将如何传递该位置,我不需要调用另一个函数来加载源页面吗?c:/etc/source是本地文件系统位置。是否要浏览本地存储的文件?包含选择选项的页面的url是什么?不,c位置只是一个随机的例子。确切的一个类似于代码/应用程序/目标和代码/应用程序/source@VikasGupta你能告诉我包含下拉列表html的文件的位置吗?我需要显示在浏览器地址栏中的位置。看,我正在为您提供每个文件的确切位置。我正在source.html文件中的位置E:\Trans\WebContent\demo\code\app\sc\LoggedIn\sc\source进行列表编码,这也是sourceC.js文件的位置,也是我要对onclick change事件进行编码的位置。另一个文件targetetc.js或target.html位于E:\Trans\WebContent\demo\code\app\sc\LoggedIn\sc\target,所以我已经向您提供了所有位置,现在请告诉我如何在source.js文件中为onclick change事件编码我正在寻找这样的答案,但仍然没有得到答案。如果我的源文件位于c:/etc/source位置,那么我将如何传递该位置,我不需要调用另一个函数来加载源页面吗?c:/etc/source是本地文件系统位置。是否要浏览本地存储的文件?包含选择选项的页面的url是什么?不,c位置只是一个随机的例子。确切的一个类似于代码/应用程序/目标和代码/应用程序/source@VikasGupta你能告诉我包含下拉列表html的文件的位置吗?我需要显示在浏览器地址栏中的位置。看,我正在为您提供每个文件的确切位置。我正在source.html文件中的位置E:\Trans\WebContent\demo\code\app\sc\LoggedIn\sc\source进行列表编码,这也是sourceC.js文件的位置,也是我要对onclick change事件进行编码的位置。另一个文件targetetc.js或target.html位于位置E:\Trans\WebContent\demo\code\app\sc\LoggedIn\sc\target,所以我已经向您提供了所有位置,现在请告诉我如何在source.js文件中为onclick change事件编码