0

I have a form like the following, I need to pass the values of checkboxes that checked I dont know how.

<input type="checkbox" id="invid" name="invid" value="100236">
<input type="checkbox" id="invid" name="invid" value="100852">
<input type="checkbox" id="invid" name="invid" value="100962">
<input type="checkbox" id="invid" name="invid" value="100965">
<input type="checkbox" id="invid" name="invid" value="101002">

<button id="submit">Submit</button>

<script>

$('#submit').click(function() {

$.ajax({
    url: "collect.jsp",
    type: "post",
    data: Winvid: invid,
    success: function(data) {
    $('#response').html(data);
    }
});


});
</script>
4
  • 1
    you cannot use same id for multiple elements Commented Sep 30, 2016 at 8:58
  • $(':checkbox:checked').val() Commented Sep 30, 2016 at 8:58
  • Id be unique in html. Commented Sep 30, 2016 at 9:01
  • 1
    @guradio unfortunately that only gets you the first checked value. You need to loop through them all Commented Sep 30, 2016 at 9:01

3 Answers 3

2

Firstly your HTML is invalid as you have repeated the id property when they must be unique. Use classes instead:

<input type="checkbox" class="invid" name="invid" value="100236">
<input type="checkbox" class="invid" name="invid" value="100852">
<input type="checkbox" class="invid" name="invid" value="100962">
<input type="checkbox" class="invid" name="invid" value="100965">
<input type="checkbox" class="invid" name="invid" value="101002">

Then you can use map() to create an array of the selected checkbox values. Then you can provide an object to the data property of the $.ajax() method to pass the data in your request. Try this:

$('#submit').click(function() {
    var checkedValues = $('.invid:checked').map(function() {
        return this.value;
    }).get();

    $.ajax({
        url: "collect.jsp",
        type: "post",
        data: { invid: checkedValues },
        success: function(data) {
            $('#response').html(data);
        }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

@Rory McCrossan I got this error: Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: invid
@Giorgos please edit your question to include your code as you've made an error somewhere. Sounds like you've got a jQuery selector wrong somewhere, like $(':invid')
@Rory McCrossan I need only the checked values, also how to get the values from the collect jsp?
Ah, my mistake. I missed the :checked selector: $('.invid:checked'). I'm not familiar with JSP, but it will be sent as a string which you can presumably split() in to an array on the server side.
1

Use check boxes as array:

 <input type="checkbox"  name="invid[]" value="100236">
    <input type="checkbox"  name="invid[]" value="100852">
    <input type="checkbox"  name="invid[]" value="100962">
    <input type="checkbox"  name="invid[]" value="100965">
    <input type="checkbox"  name="invid[]" value="101002">

.serialize() gives you a string of form encoded data.

Passing an object to data: will cause jQuery to encode the values in the form as form encoded data.

Since you have already encoded that data, you don't want to reencode it.

Pass the string directly.

data: $('.checkboxes:checked').serialize(),

Comments

0

use .each , this demo check property checked

$('#submit').click(function() {
    var arrayValue = [];
   // use name or class name
   $('input[name=invid]').each(function(){
     if($(this).prop('checked')){
       arrayValue.push($(this).val())
     }
   });

  console.log(arrayValue);
  // arrayValue use in $.ajax 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="invid" value="100236">
<input type="checkbox" name="invid" value="100852">
<input type="checkbox" name="invid" value="100962">
<input type="checkbox" name="invid" value="100965">
<input type="checkbox" name="invid" value="101002">

<button id="submit">Submit</button>

1 Comment

How to get the values from collect.jsp?

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.