2012-11-05 81 views
0

我有一個像在PHP選擇框沒有得到所選擇的值的jQuery

<select id="addressbook_user" name="addressboook_user"> 
    <?php 
     $asql = "SELECT * from demo_addressbook WHERE user_created_id IN(SELECT id FROM demo_user WHERE user_name = '$get_user_name') AND type = 1 ";       
     // $result = mysql_query($query); 
     // mysql_real_escape_string($asql); 
     $aresult = mysql_query($asql) or die (mysql_error()); 
     while($arow_list=mysql_fetch_assoc($aresult)){ 
    ?>      
    <option value="<?php echo $arow_list['guest_name']; ?>"><?php echo $arow_list['guest_name']; ?></option> 
    <?php          
     } 
    ?> 
</select> 

這裏是我的jQuery代碼來獲得該值的選擇框。

function save() { 
    alert($("#addressboook_user").val()); 
    var selectVal = $('#addressboook_user :selected').val(); 
    alert(selectVal); 
    var user = $("#addressboook_user").val(); 
}​ 

我的問題是,當選擇框有一個選定的值,那麼爲什麼我的jQuery代碼警報總是顯示未定義?

請幫我在這裏真的很沮喪。

回答

2

你沒有帶選擇框的使用ID

變化

alert($("#addressboook_user").val()); 

alert($("#addressbook_user").val()); 
+0

boook是問題budy thaks非常mutch – user1667633

+0

+簡單的例子 – Baba

+0

@Baba謝謝非常 – GBD