简体   繁体   中英

jQuery - Getting values of text input and checkboxes into one string (or array)

I have three types of items I'm trying to get into a comma-separated string (or array), which I want to display and use to form a URL later.

How can I get these three types of data into the same string or array?

  1. An existing POST string
  2. Free input from a text field with an Add button
  3. The values of a series of checkbokes

Currently, with the code I'm using, adding the form input values overrides the string, and I can't figure out how to remove a checkbox value from the string when its box is unchecked.

Here's the fiddle.

I'm using this HTML:

<div class="srs-display">existingPostString</div>

<ul id="srs" class="srs">
    <!-- START TEXT INPUT FIELD -->
    <li class="sr">
        <div class="masonry-well">
            <input id="sr-custom" type="text" placeholder="Add your own">
            <a class="add-sr-custom">Add</a>
        </div>
    </li>
    <!-- END TEXT INPUT FIELD -->
    <!-- START CHECKBOXES -->
    <li class="sr">
        <div class="masonry-well">
            <input id="srPredefined1" type="checkbox" name="srPredefined1" value="srPredefined1">
            <label for="srPredefined1" class="ts-helper">srPredefined1</label>
        </div>
    </li>
    <li class="sr masonry-item">
        <div class="masonry-well">
            <input id="srPredefined2" type="checkbox" name="srPredefined2" value="srPredefined2">
            <label for="srPredefined2" class="ts-helper">srPredefined2</label>
        </div>
    </li>
    <li class="sr masonry-item">
        <div class="masonry-well">
            <input id="srPredefined3" type="checkbox" name="srPredefined3" value="srPredefined3">
            <label for="srPredefined3" class="ts-helper">srPredefined3</label>
        </div>
    </li>
    <!-- END CHECKBOXES -->
</ul>

And here's the JS I tried so far:

$('input[type="checkbox"]').bind('change', function() {
    var srs = 'existingPostString';

    $('input[type="checkbox"]').each(function(index, value) {
        if (this.checked) {
            /*add*/ /*get label text associated with checkbox*/
            srs += ($(this).val() + ', ');
        }
    });
    if (srs.length > 0) {
        srs = srs.substring(0,srs.length-2);
    } else {
        srs = 'No checks';
    }

    $('.srs-display').html(srs);
});

$(".add-sr-custom").on('click', function() {
    var srs = 'existingPostString';

    srs +=  ',' + $('#sr-custom').val();
    $('.srs-display').text(srs);
})

I would push your string elements to an array, and then call array.join(',') on it. Like this:

var srs = [];
//each checkbox
srs.push($(this).val());

//later
var new_string = srs.join(',');

Hi man check this solution: https://jsfiddle.net/leojavier/onwkaLet/6/

var srs = [];

    $('input[type="checkbox"]').bind('change', function() {
         srs=[]
        $('input[type="checkbox"]').each(function(index, value) {
            if (this.checked) {
                srs.push($(this).val());
            }
        });

        $('.srs-display').html(srs);
    });

    $(".add-sr-custom").on('click', function() {
        $('#sr-custom').val(srs);
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM