Additional form data on form submission

Some checkboxes (this is already tested by JQuery, but I uncheck them). But I haven't tested that. See below.

Here is the javascirpt.

$('.checkbox').change(function(){
     alert('hi');
});

$('.checkbox[data-img=a]').click();
$('.checkbox[data-img=b]').click();

      

Here is the HTML.

<input type='checkbox' class='checkbox' data-img='a' value='a' name='name' />a
<input type='checkbox' class='checkbox' data-img='b' value='b' name='name' />b
<input type='checkbox' class='checkbox' data-img='c' value='c' name='name' />c
<input type='checkbox' class='checkbox' data-img='d' value='d' name='name' />d

      

I am calling cick event from jquery. Thus, a and b are checked by alerts. Before submitting the form, I uncheck a and b and validate c and d. But when the form is submitted, I have a, b, c and d. Actually, I only have to get c and d.

I am using ASP. When I catch them on the server, the following result appears.

name = Request.Form("name") ' name = "a, b, c, d"

      

When I remove this checkbox $ ('. Checkbox [data-img = a]'). click (); "it works well.

name = Request.Form("name") ' name = "c, d"

      

I do not know why? Someone please explain this. Thank.

+3


source to share


1 answer


Your attribute name

should be like name[]

. This is how it should be

<input type='checkbox' class='checkbox' data-img='a' value='a' name='name[]' />a
<input type='checkbox' class='checkbox' data-img='b' value='b' name='name[]' />b
<input type='checkbox' class='checkbox' data-img='c' value='c' name='name[]' />c
<input type='checkbox' class='checkbox' data-img='d' value='d' name='name[]' />d

      

When the form is submitted, you will receive the validation results as an array name

and you can process it in the backend.



EDIT:

Don't use a function click()

, use the lines below to make the already marked "a" and "b" values

$('.checkbox[data-img=a]').prop('checked',true);
$('.checkbox[data-img=b]').prop('checked',true);

      

+2


source







All Articles