【问题标题】:how to send value of checkbox using jquery and ajax with codeigniter如何使用 jquery 和 ajax 和 codeigniter 发送复选框的值
【发布时间】:2016-11-05 17:19:39
【问题描述】:

各位如何制作一个复选框,如果我选中它,复选框上的值将发送到数据库而无需提交表单功能

这是我的复选框:

<input id="active_banner" type='checkbox' name='active_banner' value='1' title='Active Banner' <?=($data->promotion_active == 1)? "checked":""?> />

这是我的 jquery:

<script>
$("#active_banner").click(function() {
    var id = "<?=$data->promotion_banner_id?>"
    var active = $("#active_banner").val();
    jQuery.ajax({
        type: "GET",
        url: "/ADMIN/ajax/active_banner/",
        data: {id:id,active:active},
    });

    });
</script>

我尝试过检查,但是在我的 ajax 控制器中没有发生任何事情,其中​​包含:

function active_banner(){
    $id = $this->input->get("id",true);
    $active = $this->input->get("active",true);

    $this->Control_panel_m->update_banner_active($id,$active);
    redirect('/ADMIN/'.country_code."/Edit_promotion_banner","refresh");

}

各位大佬能帮我看看怎么弄吗?

【问题讨论】:

  • 你怎么知道什么都没发生?您没有成功处理程序,也没有错误处理程序,并且正如指出的那样,重定向将发回其他控制器回显的任何内容。它不会导致您的页面重定向
  • 如果用户也取消选中该复选框但您没有检查它的状态,那么使用 click 也会触发
  • @charlietfl 你能帮我解决这个问题吗?因为当我检查时,我没有看到任何脚本在 firebug 中运行
  • 在 clcik 处理程序回调中添加控制台日志或警报,以查看它是否会触发。您没有将此代码包装在 document.ready ... 如果元素在运行时不存在,它将不会执行任何操作。然后检查浏览器开发工具网络,看看实际请求会发生什么
  • 还想知道您是否有多个具有相同 id 的复选框

标签: javascript jquery ajax codeigniter checkbox


【解决方案1】:

您需要检查是否选中了复选框,单击将触发选中和未选中事件

尝试使用类似以下的内容

<script>
$("#active_banner").on("change",function(e) {
      var id = "<?=$data->promotion_banner_id?>"
      var active = $(this).is(":checked");
      jQuery.ajax({
        type: "GET",
        url: "/ADMIN/ajax/active_banner/",
        data: {id:id,active:active},
      });
    });
</script>

【讨论】:

  • 不管检查状态...OP 需要为这两种情况更新服务器
  • 更新了代码,现在在您的代码中,问题是您发送的复选框的值对于选中/未选中状态将始终保持不变。
  • 对,但我的观点是 OP 需要解决这两种情况下的操作,因此可能还需要不同的属性。虽然还不清楚这在服务器上是如何工作的
【解决方案2】:

试试下面的

$(function(){
$("input[name='active_banner']").click(function() {
    var id = "<?=$data->promotion_banner_id?>"
    var active = $("#active_banner").val();
    if($(this).is("not(:checked)")){
        active = 0;
    };
    jQuery.ajax({
        type: "GET",
        url: "/ADMIN/ajax/active_banner/",
        data: {id:id,active:active},
        success: function(data){
           window.location = data;//redirect with javascript
        }
    });
    });
  });

php:

function active_banner(){
    $id = $this->input->get("id",true);
    $active = $this->input->get("active",true);

    $this->Control_panel_m->update_banner_active($id,$active);
    return '/ADMIN/'.$country_code."/Edit_promotion_banner";//return the url to redirect assuming country_code is a variable

}

【讨论】:

  • 停用怎么样?
  • 我已经关注了你的代码,但是当我检查了萤火虫中没有任何脚本,就像什么都没发生一样
  • 页面是否重定向?
  • 它只在第一个复选框上起作用,该复选框将输入foreach数据上的多少数据,我只能选中第一个复选框并且会起作用,但不是
【解决方案3】:

第一件事:如果你已经包含了你的 jQuery 文件,如下所示:

<script type="text/javascript" src="//code.jquery.com/jquery-1.12.3.min.js">
<script>
$(document).ready(function () {
$("#active_banner").change(function() {
    var id = "<?=$data->promotion_banner_id?>"
    var active = $("#active_banner").val();
    jQuery.ajax({
        type: "GET",
        url: "/ADMIN/ajax/active_banner/",
        data: {id:id,active:active},
    });
});
});
</script>

在上述情况下,需要关闭包含语句的&lt;/script&gt; 标签。所以 jQuery 包含应该是:

<script type="text/javascript" src="//code.jquery.com/jquery-1.12.3.min.js"></script>

jQuery 的版本可以和你使用的一样,不一定是我这里提到的。

其次,既然您说您通过 foreach 循环显示复选框,您应该在您的复选框上添加另一个属性,例如:data-id

&lt;input id="active_banner" type='checkbox' name='active_banner' value='1' title='Active Banner' &lt;?=($data-&gt;promotion_active == 1)? "checked":""?&gt; data-id="YOUR_UNIQUE_CHECKBOX_ID" class="active_banner_chk" /&gt;

其中 YOUR_UNIQUE_CHECKBOX_ID 是一个唯一值,每个复选框都不同。

然后,将你的 jQuery 修改为:

<script>
$(document).ready(function () {
$(".active_banner_chk").click(function() {
    var id = "<?=$data->promotion_banner_id?>";
    var active = 0;

    if( $(this).is(':checked') ) {
        var active = $(this).attr('data-id');
    }
    jQuery.ajax({
        type: "GET",
        url: "/ADMIN/ajax/active_banner/",
        data: {id:id,active:active},
    });
});
});
</script>

您也可以避免添加 data-id 的额外属性,而是使用您的 jQuery 和 HTML 元素,如下所示:

<script>
$(document).ready(function () {
$(".active_banner_chk").click(function() {
    var id = "<?=$data->promotion_banner_id?>";
    var active = 0;

    if( $(this).is(':checked') ) {
        var active = $(this).val();
    }
    jQuery.ajax({
        type: "GET",
        url: "/ADMIN/ajax/active_banner/",
        data: {id:id,active:active},
    });
});
});
</script>
<input id="active_banner" type='checkbox' name='active_banner' value='1' title='Active Banner' <?=($data->promotion_active == 1)? "checked":""?> class="active_banner_chk" />

根据类名调用您的 jQuery,然后使用 $this.val() 而不是 $('#active_banner').val() 确保您始终获得当前复选框的值而不是第一个复选框。

【讨论】:

  • 你为什么放var active=0?只要我包含在输入值中,我就需要将其发送为 1
  • 如果复选框没有被选中,那么它会将'active'的值发送为0。如果它被选中,那么它将发送该复选框的输入值。
猜你喜欢
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多