0

I'm trying to create a script that will save a ticked checkbox. The code I have as of now disables add & approve when read is ticked and disables read when either of add/approve is ticked. Now the problem I have is when I added ajax submission to the jquery it doesn't save the record and it no longer disables the checkboxes(assuming I ticked read).

Here's what I got so far.

$(document).ready(function(){
    $('#list-tbl tr').each(function(){

        var $this = $(this);
        var id = $('#input').val();
        $('input[type=checkbox]', $this).change(function(){
            if($('.read', $this).is(':checked')){
                $('input[type=checkbox]', $this).prop('disabled',true);
                $.ajax({
                    type: 'POST',
                    url: <?= site_url('admin/update')?>,
                    dataType: "json",
                    data: { id: id }, 
                    success: function(data) { 
                        alert("Updated");
            }
        });
                $(this).prop('disabled',false);
            } else if($('.add', $this).is(':checked') || $('.approve', $this).is(':checked')) {
                $('.read', $this).prop('disabled', true);
                $(this).prop('disabled', false);
            } else{
                $('input[type=checkbox]', $this).prop('disabled',false);
                $(this).prop('disabled',false);
            }
        });
    });
});

Ex: http://jsfiddle.net/bGatX/

I'm getting a lil bit frustrated I just want to save the data in the right manner and perform the disabling as it is....

Many thanks.

1
  • From our perspective, it's sort of difficult to identify what your issue actually is. Would you consider making a jsFiddle with the accompanying HTML? Commented Feb 14, 2012 at 22:43

1 Answer 1

1

Looking at your jsFiddle... you don't actually have a HTML element with id="input", even though, you've trying to save an id using: var id = $('#input').val();

This is the id you're trying to send to the server but it doesn't really exist. What are you really trying to do? Establish whether a certain user has read/added/approved a particular item? If that's the case what you need to send to the server is a user id combined with a string that represents whether the user read/added or approved an item.

On a minor note and as a matter of redundancy and performance: $(this).prop('disabled',false); is common amongst all conditionals and can be pulled out to the front as a common factor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.