javascript - storing multiple checkboxes value in a variable or array - Stack Overflow

so this works fineHTML<p><p><select id="single"><option>Single<

so this works fine

HTML

<p></p>
<select id="single">
    <option>Single</option>
    <option>Single2</option>
</select> 
<select id="multiple" multiple="multiple">
    <option selected="selected">Multiple</option>
    <option>Multiple2</option>
    <option selected="selected">Multiple3</option>
</select>

<div>
    <span class="label">Hobbies</span>
    <input type="checkbox" name="hobby" id="hel" value="hel">
    <label for="hel">hel</label>
    <input type="checkbox" name="hobby" id="pickle" value="pickle">
    <label for="pickle">Pickle eating</label>
    <input type="checkbox" name="hobby" id="walnut" value="walnut">
    <label for="walnut">Making walnut butter</label>
</div>

function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || [];
$( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + multipleValues.join( ", " ) );}

$( "select" ).change( displayVals );
displayVals();

/

but when I change the JQuery Selector to input it returs multipleValues.join() is not a function!

function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "input[name='hobby']:checked" ).val() || [];
$( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + multipleValues.join( ", " ) );
}

$( "input" ).change( displayVals );
displayVals();

so what am I doing wrong?

so this works fine

HTML

<p></p>
<select id="single">
    <option>Single</option>
    <option>Single2</option>
</select> 
<select id="multiple" multiple="multiple">
    <option selected="selected">Multiple</option>
    <option>Multiple2</option>
    <option selected="selected">Multiple3</option>
</select>

<div>
    <span class="label">Hobbies</span>
    <input type="checkbox" name="hobby" id="hel" value="hel">
    <label for="hel">hel</label>
    <input type="checkbox" name="hobby" id="pickle" value="pickle">
    <label for="pickle">Pickle eating</label>
    <input type="checkbox" name="hobby" id="walnut" value="walnut">
    <label for="walnut">Making walnut butter</label>
</div>

function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || [];
$( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + multipleValues.join( ", " ) );}

$( "select" ).change( displayVals );
displayVals();

http://jsfiddle/bfha4881/

but when I change the JQuery Selector to input it returs multipleValues.join() is not a function!

function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "input[name='hobby']:checked" ).val() || [];
$( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + multipleValues.join( ", " ) );
}

$( "input" ).change( displayVals );
displayVals();

so what am I doing wrong?

Share Improve this question edited May 8, 2015 at 9:16 Ziem 6,6978 gold badges56 silver badges86 bronze badges asked May 8, 2015 at 8:19 yokimotoyokimoto 401 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try

var values = [];    
$( "input[name='hobby']:checked" ).each(function(){
    values.push($(this).val());
});
var str = values.join(", ");

Also, you can concatenate directly in the "each", but remember to remove the ", " from the last loop.

For input elements, the returned value will be string, so for array of checkboxes you can iterate over checked inputs and then use .map() to create an array as below

function displayVals() {
    var singleValues = $("#single").val();
    var multipleValues = $("#multiple").val() || [];
    var hobbies = $('input[name="hobby"]:checked').map(function () {
        return this.value;
    }).get();
    $("p").html("<b>Single:</b> " + singleValues +
        " <b>Multiple:</b> " + multipleValues.join(", ") +
        " <b>Hobies:</b> " + hobbies.join(", "));
}

$("select, input").change(displayVals);
displayVals();

Demo: Fiddle

You need to get all checkboxes, you can do that with .each() function...

var checkBoxes = "";
function displayVals() {
  var singleValues = $( "#single" ).val();
  $( "input[name='hobby']:checked" ).each(function() {
        checkBoxes += $(this).val() + " ";
    });

  $( "p" ).html( "<b>Single:</b> " + singleValues +
    " <b>Multiple:</b> " + checkBoxes );
    checkBoxes = "";
}

Here is working example: jsfiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744749022a4591480.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信