Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkbox is always "on"

Hy!

When Uploadify send the file to the action, I need to know if the checbox is checked or not, so I did:

    $('#uploaded').uploadify({         'uploader': '/uploadify.swf',         'cancelImg': '/cancel.png',         'script': '/Interaction/Upload',         'multi': true,         'auto': false,         'method': 'post',         'scriptData': {'Checkbox': $('#checkbox').val()},     }); 

But I aways get an "on" value. No matter if is checked or not.

can anyone help? Tks.


UPDATE:

I realized that uploadify is getting the checkbox when the page is loaded.This means that if I change the checkbox (or any other type of input) the uploadify will get the initial value, in this case, "checkbox = false".

How can I send a form with uploadify?

Tks.

like image 432
Thiago Avatar asked Jul 06 '11 19:07

Thiago


People also ask

How do I make a checkbox always checked?

Checkbox always set to checked if using checked="@(false)" if Helper function run to set other values first.

How do you keep a checkbox in HTML?

The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.

How do you disable a checkbox if it is checked?

Using attr() function attr('disabled'); This function also takes a parameter to specify the property or the task that the selected element or checkbox is applied to. Whereas this function specifies the property for the checkbox object and it is specified as “disabled” for disabling the checkbox object.

What is a checkbox in HTML?

Definition and Usage. The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

try $('#checkbox').is(':checked')

like image 174
MikeM Avatar answered Oct 05 '22 10:10

MikeM


A checkbox's value is always 'on'. The HTML Form sends the value to the server only if the checkbox is checked. If the box is not checked, then the request parameter will be absent. This semantics is apparently to minimize the differences between checkboxes and radio buttons.

The HTML Form implements the following semantics:

if ($('#mycheckbox').is(':checked')) {     payload['mycheckbox'] = $('#mycheckbox').val(); } 

You can either mimic this semantics in Ajax or send the 'checked' flag directly as follows:

payload['mycheckbox'] = $('#mycheckbox').is(':checked'); 

I'd recommend mimic'ing the HTML Form semantics. Irrespective of what the client does, I'd recommend the following in the server code:

if mycheckbox param exists and its value is either 'true' or 'on' {  // pseudo code of course     // mycheckbox is checked } else {     // mycheckbox is unchecked } 

Hope this explanation helps.

like image 24
Gopi Reddy Avatar answered Oct 05 '22 10:10

Gopi Reddy