How to show consistent results inside a text box

Dear all.I want to use only one text field for different results. what should I do if I want, after clicking the check box or another check box several times, the results will appear sequentially in the text box. For instance:

<input type="checkbox" id="see" value="a">
<input type="checkbox" id="saw" value="b">

<input type="text" id="field">
<input type="button" id="show">

then I do something like:

 1. click "see"
 2. click "show"
 3. click "see"
 4. click "show"
 5. click "saw"
 6. click "show"
 7. click "saw"
 8. click "show"
 9. click "see"
 10. click "show"

then display the results after clicking the browse button in the text box:

aaba..and so on if any additional
+3
source share
2 answers

Easier than:

var values = "";
$('#see, #saw').click(function() {
  values += $(this).val();
});
$('#show').click(function() {
  $("#field").val(values);
});

so that only checked = true flags:

var values = "";
$('#see, #saw').click(function() {
 if($(this).is(':checked'))
    values += $(this).val();
});
$('#show').click(function() {
  $("#field").val(values);
});

Edited: to add functionality toShow Button

See working script

+2
source

Try something like this:

$('input:checkbox').click(function() {
  $('#field').val($('#field').val() + $(this).val());
});​

This is where fiddle works .

, , , :

if( $(this).is(':checked') ) {...}
+1

Source: https://habr.com/ru/post/1762583/


All Articles