复选框 - 使用 jquery 和 mysql 选中或取消选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6017558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:14:25  来源:igfitidea点击:

checkbox - checked or unchecked with jquery and mysql

ajaxjquerycheckbox

提问by Dennis Lauritzen

I am currently doing a system where it has to be possible to check/uncheck a checkbox. Everytime it changes status I need jquery to make and ajax call to a page, that updates the database.

我目前正在做一个系统,它必须可以选中/取消选中一个复选框。每次它更改状态时,我都需要 jquery 和 ajax 调用页面,更新数据库。

My problem is - how can I do this, so it updates?

我的问题是 - 我该怎么做才能更新?

Thanks in advance.

提前致谢。

回答by Sarah West

For example you can do it like this:

例如,您可以这样做:

First you have to look if the checkbox is checked:

首先,您必须查看复选框是否已选中:

$("#yourSelector").live("click", function(){
        var id = parseInt($(this).val(), 10);
        if($(this).is(":checked")) {
            // checkbox is checked -> do something
        } else {
            // checkbox is not checked -> do something different
        }
});

You can load specific content via Ajax:

您可以通过 Ajax 加载特定内容:

$.ajax({
                type: "POST",
                dataType: "xml",
                url: "path/to/file.php",
                data: "function=loadContent&id=" + id,
                success: function(xml) {
                    // success function is called when data came back
                    // for example: get your content and display it on your site
                }
});

回答by fearofawhackplanet

Which bit are you stuck on? You should probably have something like this...

你坚持哪一点?你应该有这样的东西......

$('#myCheckbox').click(function() {
    var checked = $(this).is(':checked');

    $.ajax({
        type: "POST",
        url: myUrl,
        data: { checked : checked },
        success: function(data) {
            alert('it worked');
        },
        error: function() {
            alert('it broke');
        },
        complete: function() {
            alert('it completed');
        }
    });
});

回答by Val

Something like this probably?

大概是这样的?

$('.checkbox').click(function (){
    var val = $(this).is(':checked');
    $.load('url_here',{status:val});
});

回答by thinklinux

<input type="checkbox" name="foo" value="bar" class="checkIt"/>

<script type="text/javascript">
    $('.checkIt').bind('click', function() {
        if($(this).is(":checked")) {
            // checkbox is checked
        } else {
            // checkbox is not checked
        }
    });
</script>

You can now have more than one checkbox.

您现在可以拥有多个复选框。

回答by glutorange

Detect if checkbox is checked:

检测复选框是否被选中:

if ( $('#id').is(':checked') ) { }

This can be executed in a function that is triggered by "onchange" event.

这可以在由“onchange”事件触发的函数中执行。

function checkCheckboxState() {

    if ( $('#id').is(':checked') ) { 

        // execute AJAX request here

    }
}