繁体   English   中英

如何超链接在新标签页中打开并带有一些变量的选择标签

[英]How to hyperlink a select tag which open in a new tab and carry some variables

我是PHP新手。 当用户单击选择标签的选项标签时,我想打开一个新标签。 但是我失败了。 请帮我。

这是我的代码

echo '<select name="sector_study_data" id="sector_study_data" STYLE="width: 300px">';

echo '<option value="">[--Select Sector Study2----]</option>';

    if(is_array($sector_study ) && !empty($sector_study ))
    {
        foreach($sector_study  as $sector_study)
        {
            echo '<option value="'.$sector_study->ID.'" href="update_sector_study.php?id='.$sector_study->ID.'"
            "target="_blank"">';

            //echo '<a href="http://www.w3schools.com">$sector_study->name</a>';

            echo $sector_study->name;

            echo '</option>';
        }
    }

echo '</select>';

在这里,使用jquery可以满足您的要求:

1将url添加到所有选项的data-href属性中:

echo '<select name="sector_study_data" id="sector_study_data" STYLE="width: 300px">';
echo '<option value="">[--Select Sector Study2----]</option>';

    if(is_array($sector_study ) && !empty($sector_study ))
    {
        foreach($sector_study  as $sector_study)
        {
            echo '<option value="'.$sector_study->ID.'" data-href="update_sector_study.php?id='.$sector_study->ID.'">';

            //echo '<a href="http://www.w3schools.com">$sector_study->name</a>';

            echo $sector_study->name;

            echo '</option>';
        }
    }
echo '</select>';

使用jQuery:

$("#sector_study_data").change(function(){
  var href = $('option:selected', this).attr('data-href'); // get href of selected option
  window.open(href, '_blank'); // redirect to that href in new tab
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM