Change Hidden field in form

Asked

Viewed 1,145 times

1

I have two Hidden fields inside a form they must store the values of a radio button and the selected checkboxes (150 checkboxes), I was able to make the first Hidden field load the value of the selected radio button but I was unable to make the 2nd Hidden store the various checkboxes marked

<form class="navbar-form span7 text-center" role="search" id="search-form" action="../PHP/ValidateForm.php" method="get">
                    <div class="form-group">
                        <label class="radio-inline"><input type="radio" name="result_type" value="videos" checked="">Videos</label>
                        <label class="radio-inline"><input type="radio" name="result_type" value="photos">Photos</label>
                        <label class="radio-inline"><input type="radio" name="result_type" value="tumblr">Tumblr</label>
                    </div>
                    <div class="input-group">
                        <input class="form-control" type="text" id="search_input" name="search_input" placeholder="Search" />
                        <div class="input-group-btn">
                            <button class="btn btn-default" type="submit" name="search_form_submit" value="search">Search</button>
                        </div>
                    </div>
                    <input type="hidden" name="upload-date" value="up-0" />
                    <input type="hidden" name="from" value="" />
                </form>

Radio code Buttons and checkboxes:

<div id="right_sidebar" class="col-md-2"><!-- START Right Sidebar -->
<div class="row">
    <h4>Upload Date</h4>
    <div class="radio">
        <label><input type="radio" name="upload-date" value="up-0" checked="checked">ANYTIME</label>
    </div>
    <div class="radio">
        <label><input type="radio" name="upload-date" value="up-1">Today</label>
    </div>
    <div class="radio">
        <label><input type="radio" name="upload-date" value="up-2">This Week</label>
    </div>
    <div class="radio">
        <label><input type="radio" name="upload-date" value="up-3">This Month</label>
    </div>
    <h4>From</h4>
    <div id="sites-list">
        <div class="checkbox">
            <label><input type="checkbox" name="site[]" value="site-0" checked="checked">ANYONE</label>
        </div>
        <div class="checkbox">
            <label><input type="checkbox" name="site[]" value="site-1">Site</label>
        </div>
        <div class="checkbox">
            <label><input type="checkbox" name="site[]" value="site-2">Site</label>
        </div>
        <div class="checkbox">
            <label><input type="checkbox" name="site[]" value="site-3">Site</label>
        </div>
    </div>
</div>

Code that works only for insertion of the value of the radio button to the 1º Hidden

$('input[name=upload-date]').change(function(){
    $('#search-form > input[name=upload-date]').val($(this).val());
});
  • Confirming The 1st Hidden field that stores the radio selection result is working, the 2nd that would store all the checkboxes not.

  • You will put only one value, of the 150, in an input?

  • yes are 150 options where the user can choose from 0 up to 150 options, they are not self-exclusive so he can choose all

  • Then you can stay in the input something like: up-0 up-1 up-2...?

  • @Ricardohenrique What is the reason for putting the values in these input’s? When the form is submitted you will receive the input’s radio and checkbox.

  • @Rafael input with value=up-0 up-1 up-2 are radio Buttons so there will only be one, Note: the first field Hidden is working

  • Hummmm. Then the radio will give a value for the input in the name="upload-date"?? That’s all you want in this step?

  • Another thing, it’s not that only one is working, it’s not working at all, because you set a value="up-0", in Hidden. kkk

  • @Oeslei want to put the values marked in the checkboxes inside the 2nd hidenn field

  • @Rafael when I click on another radio is assigned its value to the Hidden field

  • I get it. :)-----

  • How many are there? Only these?

  • @Rafael will be 4 Radios on the other hand will be 150 chekboxes (has a css applied to have a scroll bar)

  • @Ricardohenrique, See if this is the way. I removed Hidden, for conference, and only did the radio: http://jsfiddle.net/voyhzec4/

Show 9 more comments

2 answers

1

This query returns the first input below search-form:

$('#search-form > input[name=upload-date]')

I’d need something like:

$('#search-form input[name=upload-date]')
  • Hello @Ricardoperes, the input[name=upload-date] so I have an input with name=upload-date the other inputs have different name

  • But that’s not what’s in the code you submitted: <div class="radio"> <label><input type="radio" name="upload-date" value="up-1">Today</label> </div> <div class="radio"> <label><input type="radio" name="upload-date" value="up-2">This Week</label> </div> <div class="radio"> <label><input type="radio" name="upload-date" value="up-3">This Month</label> </div>

1


Here’s the thing, you’re duplicating the input’s through the attribute name. You must have the two input’s of type hidden with radios and checkboxes different numbers.

<!-- veja que alterei o campo name do upload-date -->
<input type="hidden" name="uploadDate" value="up-0" />
<input type="hidden" name="from" value="" />

I also suggest putting a id in these two input’s for easy selection in the script.

<input type="hidden" name="uploadDate" id="uploadDate" value="up-0" />
<input type="hidden" name="from" id="from" value="" />

$('input[name=upload-date]').change(function() {
    // apenas modifiquei o seletor
    $('#search-form #uploadDate').val($(this).val());
});

// não sou muito inclinado a usar seletores para campos com
// name indicando um array, por isso deixo uma alternativa
$('#sites-list :checkbox').change(function() {
    // array para os checkboxes selecionados
    var arrSel = [];
    $('#sites-list :checkbox').each(function() {
        if ($(this).is(':checked')) {
            // adiciona ao array
            arrSel.push($(this).val());
        }
    });
    // junta tudo no input desejado
    $('#from').val(arrSel.join(','));
    // o input ficará com os valores separados por vírgula
    // ex: site-1, site-2
}
  • ,@Could Oeslei switch ('#sites-list :checkbox) to (input[name=site])? worked perfectly

  • @Ricardohenrique Works by putting this selector? You can edit my question if you want. I never use selectors by attribute name when they are arrays. I usually use the id =)

  • Okay, thanks for the answer. by the way with <input type="Hidden" name="upload-ate" value="up-0" /> was working even with the name of this Hidden equal to the name of the radio Buttons

Browser other questions tagged

You are not signed in. Login or sign up in order to post.