2012-04-09 56 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>