2012-04-09 91 views
1

jQuery無法在下拉菜單中找到我的自定義屬性我有一個類似的只有一行上面,它很好,但這不斷給我一個未定義。jQuery找不到自定義屬性

的jQuery

$('#admin_student_school_select').change('click',function(){ 
var school_student = $(this+':selected').attr('school_student'); 
$('#admin_student_content_details') 
    .html(ajax_load) 
    .load(loadUrl,"form_being_submitted=admin_page_list_student&school="+school_student); 
}); 

HTML

<select id="admin_student_school_select"> 
    <option>Select a School</option> 
    <option school_student="1">Riverside Community College</option> 
    <option school_student="2">Victor Valley College</option> 
    <option school_student="3">Cal State San Bernardino</option> 
    <option school_student="4">Craffton College</option> 
    <option school_student="5">San Bernardino Community</option> 
</select> 

Ajax調用作品背後的腳本。我回應了結果。

+0

是什麼'$(這+ ':選擇')'評估爲?在這種情況下,「this」不是DOM元素嗎? – 2012-04-09 05:08:16

+0

你能告訴我們哪個變量是「undefined」嗎? – 2012-04-09 05:14:42

+0

School_student。它找不到school_student屬性值。 – Cjueden 2012-04-09 05:16:15

回答

3

此行是無效的:

$('#admin_student_school_select').change('click',function(){ 

這是什麼,一個clickchange事件?


更新:

$(this+':selected')不是一個有效的選擇......使用:

var school_student = $(':selected', this).attr('school_student'); 
+0

我得到'click'的騎,它仍然沒有' t工作 – Cjueden 2012-04-09 05:13:34

+0

@Cjueden。我更新了答案.' $(this +':selected')'不是一個有效的選擇器。你想要選擇什麼? – gdoron 2012-04-09 05:15:59

+0

我想要在下拉菜單中選擇曾經選擇的school_student屬性值 – Cjueden 2012-04-09 05:17:42

2

我不認爲$(this+':selected')將返回有用的東西。

嘗試:

$('#admin_student_school_select').change(function(){ 
    var school_student = $(':selected', this).attr('school_student'); 
    // rest of your code 
}); 
+0

你是對的!它會導致錯誤。但你可以改進選擇器:'$(':selected',this).attr('school_student');' – gdoron 2012-04-09 05:18:15

+0

@gdoron感謝堆!將編輯:) – 2012-04-09 05:18:51

+0

** + 1 **它會導致這種錯誤:'語法錯誤,無法識別的表達式:[對象HTMLSelectElement]:選擇' – gdoron 2012-04-09 05:21:33

0

試試這個:

$('#admin_student_school_select').change(function(){ 
    var school_student = $(this).val(); 
}); 

<select id="admin_student_school_select"> 
    <option value="0">Select a School</option> 
    <option value="1">Riverside Community College</option> 
    <option value="2">Victor Valley College</option> 
    <option value="3">Cal State San Bernardino</option> 
    <option value="4">Craffton College</option> 
    <option value="5">San Bernardino Community</option> 
</select>