2013-04-04 69 views
2

我的复选框后,在codeigniter JavaScript中执行后操作后返回false值。如何保持复选框保留后检查后功能

这里是我的功能subjectid_change代码:

function subjectid_change(id, subjectid){ 
    setValue(id, subjectid); 
    var field = '#ch' + id; 
    set_credithour(field, subjectid); 
} 

$("select[name^=subjectid]").change(function(){ 
var subjectid = $(this).val(); 
set_credithour($(this).parent('td').find('input'), subjectid); 
$(this).parent().find('input').attr('name', 'credithour['+subjectid+']'); 
$(this).parent().next()find('input').attr('name', 'gradepoint['+subjectid+']'); 
$(this).parent().next().next().find('input').attr('name', 'cgpacalc['+subjectid+']'); 
}); 

我试图e.preventDefault();但它阻止我要改变以前的值。 我也尝试过使用.prop("checked"),结果仍然相同。

此代码为功能subjectid_change:

function set_credithour(field, subjectid){ 
    $.post('<?php echo site_url('academic/ajax_get_subject_credit'); ?>', {subjectid:subjectid}, 
    function(response){ 
     if(response.success){ 
      $(field).val(response.value); 
     } else{ 
      alert('The subject you entered has no credit hour.'); 
     } 
    }, 'json'); 
} 

所以,我应该怎么做,如果我想保持复选框做一个$.post检查后。

在控制器:

function ajax_get_subject_credit() 
{ 
    $subjectid = $this->input->post('subjectid'); 
    $this->db->select('subjectid, subjectcredit'); 
    $this->db->where('subjectid',$subjectid); 
    $query = $this->db->get('ref_subject'); 
    $credithour = $query->row()->subjectcredit; 
    $query->free_result(); 
    $result = $credithour; 
    echo json_encode(array('success' => true, 'value' => $result)); 

} 

当我选择的对象,学分会自动填入,并点击复选框同意计算CGPA。但是,当我添加新主题后单击计算按钮时,计算过程不运行,复选框返回false。所以,我必须再次点击复选框并按下Calculate按钮才能使过程正常工作。这使得用户做两次相同的工作。如何解决这个问题?

+0

你可以发布完整的代码,并显示演示? – Joseph 2013-04-04 03:18:27

+1

你好像在使用JavaScript?您的发布数据位于您的PHP代码中,并确定复选框是否已选中,您将需要使用php访问发布数据。 – Jeemusu 2013-04-04 03:20:14

+0

我已更新我的代码。 – Ae98 2013-04-04 03:31:12

回答

0

这里是我的结果:

我都看上了有岗位后一定误差,其复选框自动重命名以gradepoint而不是cgpacalc。

var subjectid = $(this).val(); 
    set_credithour($(this).parent('td').next().find('input'), subjectid); 
    $(this).parent().find('input[name^=subjectstudentid]').attr('name', 'subjectstudentid['+subjectid+']'); 
    $(this).parent().parent().find('input[name^=credithour]').attr('name', 'credithour['+subjectid+']'); 
    $(this).parent().parent().find('input[name^=gradepoint]').attr('name', 'gradepoint['+subjectid+']'); 
    $(this).parent().parent().find('input[name^=cgpacalc]').attr('name', 'cgpacalc['+subjectid+']'); 
1
$.post('url',function(){ 
    $('checkbox').attr('checked','checked'); 
    /*here go on with all your stuffs*/ 

    }); 
+0

我试过这种方法,但复选框保持未选中之后做一个后期处理 – Ae98 2013-04-08 03:14:33

+0

真的很奇怪,有没有任何js绑定到该复选框,然后再调用发布请求? – sbaaaang 2013-04-08 13:00:47

+0

我可以使用初始语句发布复选框值以发布功能吗?如果可以,如何将复选框的值放入'$ .post('<?php echo site_url('academic/ajax_get_subject_credit');?>',{subjectid:subjectid},' – Ae98 2013-04-12 07:54:24