2016-05-20 14 views
0

私はPHPで新しいです。ユーザーがSelect Tag Option Valueをクリックすると、新しいタブを開きたいここ は私のコード選択タグの新しいタブを開く方法

<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> 

は私がエラーを持っている私のコードであるです。オプションタグを最初にクリックしたとき。コードが動作しません。しかし、私は開いた新しいタブをもう一度クリックします。あなたは、発射機能を持っているあなたはイベントリスナー(.change())を持っている私のエラー

+0

はい私はそれを試みたが、動作していない – sunny

答えて

1

あなたが二回変更イベントを置くためです:

例えば、DOMは( $(function(){ })を使用して)準備ができたときにイベントリスナーを追加する -

は一つだけを使用してみてください

  • あなたのhtmlタグに入れたら<select .... onchange="open_window(this.value)>"
  • そして、あなたのjsコード$("#select_ss").change(function(){});

たとえば、jsのものを削除してみてください。

1

を削除するために私を助けてください。

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

See JSFiddle

関連する問題