0

The following code is made of JQuery 1.3. How can i get it to work with the latest version: 1.9.1 I also need it to remove the checked checkbox when the delete link is clicked. I think I should use the remove() function for that.

The checkboxes is like this:

<input type="checkbox" name="prog" value="C">
$(document).ready(function()
    {
        $("#submit_prog").click(
                function()
                {
                    var query_string = '';

                    $("input[@type='checkbox'][@name='prog']").each(
                        function()
                        {
                            if(this.checked)
                            {
                                query_string += "&prog[]=" + this.value;
                            }
                        });

                    $.ajax(
                        {
                            type: "POST",
                            url: "post_prog.php",
                            data: "id=1" + query_string,
                            success: 
                                function(t) 
                                {
                                    $("div#content").empty().append(t);
                                },
                            error:
                                function()
                                {
                                    $("div#content").append("An error occured during processing");
                                }
                         });    
                });
    });

EDIT: Can I do this to remove the checkbox on delete:

function(event)
{
   if(this.checked)
    {
    query_string += "&prog[]=" + this.value;
    $(this).remove();
        }
}
1
  • The checkboxes is like this: <input type="checkbox" name="prog" value="C"> Commented Apr 24, 2013 at 17:27

3 Answers 3

2

I think this line is supposed to read

$("input[@type='checkbox'][@name='prog']")

as

$("input[type='checkbox'][name='prog']")

Also a good idea to change the signature of the event handler

 $("#submit_prog").click(function () {

to

 $("#submit_prog").on('click', function () {
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, what about the remove function?
1

Note that you can select only :checked checkboxes to eliminate the need for your if statement.

$(document).ready(function() {
    $("#submit_prog").on("click", function () {
         var query_string = '';

         $("input[type='checkbox'][name='prog']:checked").each(function () {
             query_string += "&prog[]=" + this.value;
             $(this).remove();
         });

         $.ajax({
             type: "POST",
             url: "post_prog.php",
             data: "id=1" + query_string,
             success: function(t) {
                 $("div#content").empty().append(t);
             },
             error: function() {
                 $("div#content").append("An error occured during processing");
             }
         });    
    });
});

Comments

0

1- Remove '@' from the selectors

2- Use jQuery.on('click', function(){} instead of jQuery.click() function

3- Use the jQuery promise functions:

$.ajax({
    type: "POST",
    url: "post_prog.php",
    data: "id=1" + query_string
})
    .done(function (t) {
    $("div#content").empty().append(t);
})
    .error(function () {
    $("div#content").append("An error occured during processing");
});

2 Comments

Okay, and the checkbox remove function?
It should work find when you remove '@'. or you can add the :checked pseudo selector: <pre> $("input[type='checkbox'][name='prog']:checked").each(function () { query_string += "&prog[]=" + this.value; $(this).remove(); }); </pre>

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.