我是 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>
在我的代码中我有一个错误。当我第一次单击选项标签时。代码不起作用。但我第二次单击它打开的新选项卡。请帮助我消除错误
最佳答案
这是因为你放置了两次更改事件:
- 一旦进入您的 html 标签
<select .... onchange="open_window(this.value)>"
- 一旦进入你的js代码
$("#select_ss").change(function(){});
例如,尝试删除 js 中的那个。
关于javascript - 如何在选择标签上打开新选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37345076/