在下面的代码中,我有一个学生表,当我想要更新时,我会从coursesubject表中选择一个下拉course_code,它应该显示来自coursesubject的相应subject_code。但是我的实际结果是它显示了courseubject在coursecode中的所有值。主题代码。请任何人帮助我。
查看:student_detail_view
<section id="tables">
<script>
jQuery(document).ready(function() {
oTable = jQuery('#studenttable').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
});
</script>
<script type="text/javascript" charset="utf-8">
function get_studentdetails() {
var exam_name = jQuery('#exam_name_id').val();
//alert("exam_name"+exam_name);
jQuery.ajax({
data: {
exam_name: exam_name,
},
type: 'POST',
url: 'student_site/studentupdate',
success: function(data){
//alert("inside change");
console.log(data);
jQuery('#details').html(data);
jquery('#deleting').html(data);
}
});
jQuery(document).ready(function(){
jQuery.ajax({
data: {
exam_name: exam_name,
},
type: 'POST',
url: 'student_site/studentcreate',
success: function(data){
//alert("inside change");
console.log(data);
jQuery('#crud').html(data);
jquery('#deleting').html(data);
}
});
});
}
function CheckBoxVerification(From)
{
alert('hai');
alert('hai:' $("table input[type=checkbox]:checked").length);
if(From == "Insert")
{
if(!!$('#rowInsert input:checked').length == false)
{
alert('please select at least one check box to Create ...!');
}
}
if(From == "Update")
{
if(!!$('#rowUpdate input:checked').length == false)
{
alert('please select at least one check box to Update ...!');
}
}
}
function get_subjectdetails() {
//var index = jQuery('#index').val();
var course_name = jQuery('#course_name_id').val();
//alert("course_name"+course_name);
var exam_name = jQuery('#course_name_id>option:selected').text();
var exam_name = jQuery('#exam_name_id').val();
var ssubject_code = jQuery('#ssubject_code_id').val();
//var partsArray = exam_name.split('.');
//alert("ssubject_code"+ssubject_code);
//alert("course_name"+course_name);
//alert("exam_name"+exam_name);
jQuery.ajax({
data: 'exam_name='+exam_name+'&course_name=' + course_name,
type: 'POST',
url: 'student_site/subject_records',
success: function(data){
//alert("inside change");
console.log(data);
//alert ("data"+data);
//for(var j = course_name; j < ssubject_code; j++)
//{
jQuery('#ssubject_code').empty().append(data);
//}
}
});
}
</script>
<?php
$attributes=array(
'name'=>'updatecustomer',
'id'=>'updatecustomer'
);
echo form_open('student_site/manage_student',$attributes);
?>
<div id="validation_failed">
<?php
echo validation_errors();
?>
<?php $data = array();
foreach ($course_records as $row)
{
$data[$row->course_code] = $row->course_code;
}
$subject_data = array();
foreach ($all_coursesubject_records as $row)
{
$subject_data[$row->subject_code] = $row->subject_code;
}
?>
<div id="Processy">
<table class="display table table-bordered table-striped" id='studenttable'>
<thead>
<tr font style='font-size:13px'>
<th> </th>
<th> </th>
<th>Register Number</th>
<th>Name </th>
<th>Course Code</th>
<th>Subject Code</th>
</tr></thead>
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr >
<td>
<?php echo anchor('student_site/delete/'.$row->id, 'Delete',array('onClick'=>"return confirm('Are you sure want to delete..?')")); ?>
</td>
<td id="rowUpdate">
<input type=checkbox name="editstudent[]" id="editstudent[]" value="<?php echo $row->id ?>">
</td>
<td ><input class="inputmedium span2" type="text" name="register_number_<?php echo $row->id ?>" id="register_number_<?php echo $row->id ?>" value="<?php echo $row->register_number; ?>" ></td>
<td ><input class="inputmedium span2" type="text" name="name_<?php echo $row->id ?>" id="name_<?php echo $row->id ?>" value="<?php echo $row->name; ?>" ></td>
<td >
<?php
$js = 'class="dropdown_class" id="course_code_id'.$row->id.'" onChange="get_subjectdetails()" ';
$js_name = 'course_code_id'.$row->id;
echo form_dropdown($js_name, $data, $row->course_code, $js);
?>
</td>
<td>
<?php
$js = 'class="dropdown_class" id="subject_code_id'.$row->id.'"';
$js_name = 'subject_code_id'.$row->id;
echo form_dropdown($js_name, $subject_data, $row->subject_code, $js);
?>
<div id="ssubject_code" ></div>
<input type="hidden" name="ssubject_code" id="ssubject_code" value="ssubject_code"/>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<center>
<br /><input type="submit" class="btn-success btn" value="Update Student"></center>
<?php else : ?>
<h2>No records were returned.</h2>
<?php endif; ?>
答案 0 :(得分:0)
那是因为你正在加载你的主题和课程,如果你想在选择课程后加载你的主题,你可以做的是把你的主题放入这样的div(或tbody)标签
<div class="subject"></div>
并在你的jquery中:
$("#course_code_id").live("change keypress",function(){
var id = 0;
id = $(this).val();
if( $(this).val() !==''){
$.post('<?php echo site_url('subject/search_by_course') ?>',
{
subject_id: id
},function(data){
$(".subject").html( data['html_string']);
},"JSON"
);
}
});
控制器中的
function search_by_course()
{
$this->load->model('subject_model');
$id = $this->input->post('subject_id');
//get your data from model
$res_subject = $this->subject_model->subject_list($id);
$html_string = "";
$html_string .= "<select id='subject_code_id'>";
for($x=0;$x<count($res_subject);$x++)
{
$html .= "<option id=".$res_subject[$x]->subject_id.">".$res_subject[$x]->subject_name."</option>";
}
$html_string .= "</select>";
echo json_encode(array('html_string'=>$html_string));
}
通常,我这样做,到目前为止它都有效。