提交打开多个页面时,在新选项卡中打开选择选项

时间:2016-12-09 21:25:39

标签: javascript jquery

使用下面的代码...尝试在新选项卡中打开菜单项。如果我改变了多个,那么我改变的任何内容也会打开。换句话说,我只想在提交时在新标签页中打开选项。

JS:

<script type = "text/javascript">
    jQuery(function() {
        jQuery('#dynamic_select').on('change', function() {
            var url = jQuery(this).val(); // get selected value
            if (url) { // require a URL
                jQuery('#select-link').click(function() {
                    window.open(url,'_blank');
                });
            }
            return false;
        });
    });
</script>

HTML:

<select id="dynamic_select">
    <option value="" selected>Pick a Website</option>
    <option value="http://www.google.com">Google</option>
    <option value="http://www.youtube.com">YouTube</option>
    <option value="https://www.gurustop.net">GuruStop.NET</option>
</select>

2 个答案:

答案 0 :(得分:1)

我认为你想要的更接近这一点:(假设选择链接是一个按钮或应该在页面上其他地方点击的东西)

&#13;
&#13;
<script type = "text/javascript">
    jQuery('#select-link').click(function() {
      if (jQuery('#dynamic_select').val() != '')
      {
          window.open(jQuery('#dynamic_select').val(),'_blank');
      }
    });
</script>
&#13;
<select id="dynamic_select">
    <option value="" selected>Pick a Website</option>
    <option value="http://www.google.com">Google</option>
    <option value="http://www.youtube.com">YouTube</option>
    <option value="https://www.gurustop.net">GuruStop.NET</option>
</select>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我能够解决以下问题:

的jQuery

jQuery(function() {
    jQuery('#select-link').click(function(){
        var selectLink = jQuery( "#dynamic_select option:selected" ).val();
        window.open(
            selectLink,
            '_blank'
        );
    });
});

HTML:

<form>
    <span class="label-text">Visit Industry Programs:</span>
    <input type="submit" name="Submit" value="Go!" id="select-link">
    <select name="dynamic_select" id="dynamic_select">
        <option value="" selected>Select Site</option>
        <option>Select an industry</option>
        <option>Select an industry</option>
        <option>Select an industry</option> 
    </select>
</form>