【发布时间】:2013-03-25 23:29:04
【问题描述】:
在 codeigniter javascript 中执行 post 操作后,我的复选框返回 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。因此,我必须再次单击复选框和计算按钮以使该过程正常工作。这使用户两次做同样的工作。如何解决这个问题?
【问题讨论】:
-
你能发布完整的代码并展示一个演示吗?
-
您似乎在使用javascript?您的帖子数据在您的 PHP 代码中,要确定复选框是否被选中,您需要使用 php 访问帖子数据。
-
好的,我明白了,所以你在做一个 AJAX 帖子......它返回什么数据?您可能还需要发布您的
ajax_get_subject_credit函数。 -
我在代码中添加了一些流程。
-
您不能使用
.val()将复选框设置为选中状态。如果你使用 jquery > 1.6,你可以做类似$(field).prop('checked', true);
标签: javascript codeigniter post checkbox checked