简体   繁体   English

单击父div触发复选框提交?

[英]click on a parent div to trigger checkbox submission?

I have a grid of checkboxes to turn off and on permissions a group of users, I would like to be able to click on the parent div for each checkbox to fire the ajax script instead of having to click on the checkbox itself. 我有一组复选框可以关闭和打开一组用户的权限,我希望能够单击每个复选框的父div来触发ajax脚本,而不必单击复选框本身。 Instead of stating ('.permission-box') could I state parent of permission-box somehow? 除了声明('.permission-box'),我可以以某种方式声明权限框的父级吗?

Thanks 谢谢

<script>
    //<![CDATA[ 
    $(function() {
        $('.permission-box').click(function() {
            //this is what goes into $_POST
            var data = 'single_permission=' + $(this).attr('value') + '&status=' + $(this).is(':checked');
            $.ajax({
                //This would be the url that would handle updating the MySQL Record
                url: "pages/Permissions_action.php",
                cache: false,
                type: 'POST',
                data: data,
                success: function(response) {}
            });
        });
    }); //]]>
</script>

There are numerous ways to write this, anyway here is one of them. 有很多写方法,无论如何,这里就是其中一种。

First bind the click event to the parent of permission boxes. 首先将click事件绑定到权限框的父级。 Then inside the event handler don't forget to obtain the child checkbox again, and to check/uncheck that one. 然后,在事件处理程序中不要忘记再次获取子复选框,并选中/取消选中该子复选框。

<script>
    //<![CDATA[ 
$(function(){
$('.permission-box').parent().on('click', function() {
    // Obtain checkbox:
    var checkbox = $(this).find(".permission-box");

    // Switch checked state
    checkbox.attr('checked', !checkbox.is(':checked'));

    // this is what goes into $_POST
    var data = 'single_permission='+ checkbox.attr('value') +'&status=' + checkbox.is(':checked');
    $.ajax({
        //This would be the url that would handle updating the MySQL Record
        url: "pages/Permissions_action.php",
        cache: false,
        type: 'POST',
        data: data,
        success: function(response) {
         }
    });
});
});//]]> 
</script>

You could use $('.permission-box').parent().click() 您可以使用$('。permission-box')。parent()。click()

You should also look at the page of jQuery On(). 您还应该查看jQuery On()的页面。 If I am correct that is the right way to create event listners in jQuery. 如果我是正确的,那是在jQuery中创建事件列表的正确方法。

Or you could use a label-tag as div, and style it that way. 或者,您可以将label-tag用作div,然后以这种方式设置样式。

<label for="checkbox1">
     <span>Some text and/or images</span>
     <input type="checkbox" value="myValue" id="checkbox1" />
</label>

That could do the trick without javascript. 如果没有javascript,就可以解决问题。

Here we are going to get the parent of all elements with the class .permission-box and then, bind a namespaced click event. 在这里,我们将使用.permission-box.permission-box所有元素的父级,然后绑定一个命名空间的click事件。

Event namespacing is a technique that allows us to handle tasks differently depending on the event namespace used, and it is very useful when you have attached several listeners to the same event, and need to do something with just one of them. 事件命名空间是一种技术,它使我们可以根据所使用的事件名称空间来不同地处理任务,当您将多个侦听器附加到同一事件,并且只需要对其中一个进行某些操作时,此方法非常有用。

Take a look at these links: 看一下这些链接:

Using jQuery 1.7+ 使用jQuery 1.7+

$(function() { //document.ready

    //We get all parent elements to delegate the click handler
    $('.permission-box').parent().addClass("parent-permission-box");

    //Only one event handler is assigned to all matched elements.
    //Create namespaced events is a good practice.
    $(document).on("click.parentBox", ".parent-permission-box", function() {

        //triggers the click on the checkbox
        var chk = $(this).find('.permission-box'),
            status = !chk.is(':checked'),
            data;

        //toggle the checked state
        chk.attr('checked', status);

        //build the data to send
        data = { 'single_permission': chk.val(), 'status': status };

        $.ajax({
            url: "pages/Permissions_action.php",
            type: 'POST',
            data: data, //buildUrlParams(data)
            success: function (response) {
                //... or use the deferred .done()
            }
        }); //end .ajax
    }); //end .click

});

//Creates the parameters object in a querystring
function buildUrlParams (obj) {
    var prop, parameters = [];
    for (prop in obj) {
        parameters.push(prop + "=" + obj[prop])
    }
    parameters = parameters.join("&");
    return parameters;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM