【问题标题】:How to keep checkbox to stay checked after post function发布功能后如何保持复选框保持选中状态
【发布时间】: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


【解决方案1】:
   $.post('url',function(){
    $('checkbox').attr('checked','checked');
    /*here go on with all your stuffs*/

    });

【讨论】:

  • 我试过这个方法,但是在做一个后期处理后复选框保持未选中
  • 真的很奇怪在你调用post请求之前有没有绑定到那个复选框的js?
  • 我可以使用初始语句将复选框值发布到发布函数吗?如果可以,如何将复选框值放入$.post('&lt;?php echo site_url('academic/ajax_get_subject_credit'); ?&gt;', {subjectid:subjectid},
  • @Ae98 复选框值为 $('checkbox').val();您可以根据需要传递它,也可以从您发布的网址中传递
  • 你可以试试 $.post('', {subjectid:subjectid,checkbox:$('checkbox')。 val()},
【解决方案2】:

这是我的结果:

我发现发布后有一些错误是复选框自动重命名为等级点而不是 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+']');

【讨论】:

    猜你喜欢
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2016-06-19
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多