2013-04-04 71 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