为什么onClick()无法在Chrome浏览器上运行?

时间:2015-02-13 15:27:46

标签: jquery

当html选项更改时,我使用onClick()方法调用函数。它在Mozila Firefox中运行良好,但为什么它不能使用Chrome Broser?

<select name="project_list" class="project_list">
    <option>--Select Project--</option>        
    <?php 
    $getProjectList = mysqli_query($link, "SELECT p_id, p_name FROM projects WHERE p_owner_id = '$logged_user_id' ");
    while($list=mysqli_fetch_array($getProjectList)){
        $list_p_id = $list['p_id'];
        $list_p_name = $list['p_name'];
      ?>
        <option onclick="showProjectDetails(<?php echo $list_p_id; ?>);"><?php echo $list_p_name; ?></option>";
    <?php
    }
    ?> 

注意:此onclick()方法从html选项更改中获取id值并传递给showProjectDetails()

更新:

<table border="0" cellpadding="0" cellspacing="0" width="570">
<tr>
  <td width="142">Project</td> 
  <td><input type="text" id="project_name" name="p_name" value="<?php echo $p_name; ?>" /></td>
    <td colspan="1" align="right">
    <select name="project_list" class="project_list">
      <option>--Select Project--</option>  
      <?php 
      $getProjectList = mysqli_query($link, "SELECT p_id, p_name FROM projects WHERE p_owner_id = '$logged_user_id' ");
      while($list=mysqli_fetch_array($getProjectList)){
          $list_p_id = $list['p_id'];
          $list_p_name = $list['p_name'];
          ?>
          <option onclick="showProjectDetails(<?php echo $list_p_id; ?>);"><?php echo $list_p_name; ?></option>";
          <?php
      }
      ?> 
    </select>
    </td>
    <td>
      <a href="#" class="normalSearch">Normal Search</a> <br> <a href="#" class="advanceSearch">Advanced search</a>
    </td>
</tr>
<tr>  
  <td width="142">For</td> 
  <td colspan=""><input type="text" id="project_company" name="company_name" value="<?php echo $p_c_name; ?>" /></td>
  <td></td>  
</tr>
</table>

更新2:

function showProjectDetails(e){
var id = $(e).val();
$.ajax({
type:"post",
url:"showProjectDetails.php",
data:"id="+id,
    success:function(res){        
        $('#showProjectForm').html(res);        
    }
}); 
}

2 个答案:

答案 0 :(得分:3)

我认为<option>元素无法获得点击次数。见this fiddle。单击选项不会产生任何效果。

您想要观看<select>元素的更改:

$('select.project_list').on('change', function() {
  alert( this.value ); // or $(this).val()
});

答案 1 :(得分:1)

您需要将该功能附加到select代码而不是option代码。试试这个:

<select name="project_list" class="project_list" onchange="showProjectDetails(this);">
    <option>--Select Project--</option>        
    <?php
    $getProjectList = mysqli_query($link, "SELECT p_id, p_name FROM projects WHERE p_owner_id = '$logged_user_id' ");
    while ($list = mysqli_fetch_array($getProjectList)) {
        $list_p_id = $list['p_id'];
        $list_p_name = $list['p_name'];
        ?>
        <option value="<?php echo $list_p_id; ?>"><?php echo $list_p_name; ?></option>";
        <?php
    }
    ?> 
</select>
<script>
    function showProjectDetails(e) {
        var val = $(e).val();
        alert(val);
    }
</script>