【问题标题】:How to Open New Tab on Select Tag如何在选择标签上打开新标签
【发布时间】:2016-09-17 14:58:15
【问题描述】:

我是 PHP 新手。当用户单击“选择标记选项值”时,我想打开新选项卡。 这是我的代码

<td> <select name="select_ss" id="select_ss" STYLE="width: 300px"  onchange="open_window(this.value)">
                <option value="">[--Select Sector Study------]</option>
                <?php
                if(is_array($getAllSectorStudys) && !empty($getAllSectorStudys))
                {
                    foreach($getAllSectorStudys as $getAllSectorStudy)
                    {
         echo '<option value="'.$getAllSectorStudy->ID.'" data-href="test.php?id='.$getAllSectorStudy->ID.'">';
                        /*echo '<option value="'.$getAllSectorStudy->ID.'"';
                        echo '>';*/
                        echo $getAllSectorStudy->name;
                        echo '</option>';
                    } 
                }           
                ?>
            </select>

这里是 JavaScript

<script>
function open_window(myid)
{
 $("#select_ss").change(function(){
  var href = $('option:selected', this).attr('data-href');
  window.open(href, '_blank');
})
};

</script>

在我的代码中我有一个错误。当我第一次单击选项标签时。代码不起作用。但我第二次点击它打开的新标签。请帮我删除我的错误

【问题讨论】:

  • 你试过在选项中使用&lt;a href="/test.php..."&gt;标签吗?
  • 是的,我试过了,但没用

标签: javascript php jquery


【解决方案1】:

这是因为你放了两次 change 事件:

  • 在您的 html 标记中出现一次 &lt;select .... onchange="open_window(this.value)&gt;"
  • 在你的 js 代码里有一次 $("#select_ss").change(function(){});

例如,尝试删除 js 中的那个。

【讨论】:

    【解决方案2】:

    你有一个触发的函数,并且你有一个事件监听器 (.change())。

    尝试只使用一个 - 例如,在 DOM 准备好时添加事件侦听器(使用 $(function(){ })):

    $(function(){
      $("#select_ss").change(function(){
        var href=$("#select_ss > option:selected").attr("data-href");
        window.open(href,"_blank");
      })
    })
    

    See JSFiddle

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 2015-12-15
      • 2022-07-06
      • 1970-01-01
      • 2011-12-16
      • 2021-11-24
      • 2011-09-19
      相关资源
      最近更新 更多