【发布时间】:2012-06-14 20:49:03
【问题描述】:
我正在尝试使用基于 jQuery 的 .change 函数动态创建的 <select> 元素创建一个表单。我想要三个 <select elements> 选项值根据用户在前一个元素中选择的内容而变化。
我有这个代码。
students.php:
<form action='../insert.php' method='post' name='add_student'>
<!-- This is generated by PHP -->
<select id='add_student_customer'>
<option>Select one...</option>
<option value='1'>First value</option>
<option value='2'>Second value</option>
<option value='3'>Third value</option>
</select>
<!-- Here a select element will load from another PHP-file based on values from the previous select -->
<div id='add_student_contactperson_container'></div>
<!-- This is generated on page load but will not be visible until we select something in the previous select -->
<select id='add_student_course'>
<option>Select something...</option>
<option value='1'>An option</option>
<option value='2'>A second option</option>
</select>
</form>
jQuery:
<script>
$(document).ready(function() {
$("#add_student_course").hide()
$("#add_student_customer").change (function(){
var row_id = $(this).val();
$.ajax({
url: "/load_customer_contacts.php",
data: { 'customer_id':row_id },
success: function(data){
$("#add_student_contactperson_container").html(data);
}
});
});
$("#add_student_contact").change(function() {
$("#add_student_course").show();
});
});
</script>
但是$("#add_student_course").show(); 事件不起作用。任何想法为什么?另外我想知道,我能否使用从load_customer_contacts.php" 加载到我的<form> 的<select>-id?即使在页面加载之后 加载它也会跟随吗?
【问题讨论】:
-
id为
add_student_course的元素定义在哪里? -
@DvirAzulay 是
add_student_course要定义的变量或函数吗?