简体   繁体   中英

Get values of selected checkboxes and their associated textareas in Javascript

I have a HTML form. In this form I have 5 checkboxes and each checkbox has an associated textarea. So I want to get the values of only the checked checkboxes and their associated textarea text. I am able to get the values of the checked checkboxes but I can't figure out a way to get the values of their associated textarea.

My HTML code is:

<form class="my-form" id="id_MyForm">

<div class="form-group">
  <label class="qquestion">6. Which of the following is most relevant to you and why? (Select all that apply).</label>
    <div class="checkbox" id="id_Question6">
  <label>
    <input type="checkbox" id="input_que6a" value="Time" name="question6" />Time
  </label>
  <textarea name="question6a_textarea" id="id_que6a" class="form-control" placeholder="Please provide the reason"></textarea>
  <br>

  <label>
    <input type="checkbox" id="input_que6b" value="Money" name="question6" /> Money
  </label>
  <textarea name="question6b_textarea" id="id_que6b" class="form-control" placeholder="Please provide the reason"></textarea>
  <br>

  <label>
    <input type="checkbox" id="input_que6c" value="Family" name="question6" /> Family
  </label>
  <textarea name="question6c_textarea" id="id_que6c" class="form-control" placeholder="Please provide the reason"></textarea>
  <br>

  <label>
    <input type="checkbox" id="input_que6d" value="Hobbies" name="question6" /> Hobbies
  </label>
  <textarea name="question6d_textarea" id="id_que6d" class="form-control" placeholder="Please provide the reason"></textarea>
  <br>

  <label>
    <input type="checkbox" value="Other" name="question6" id="other_que6" /> Other
  </label>
  <br>
  <textarea name="question6_textarea" id="id_Question6_textinput" class="form-control" placeholder="Please elaborate"></textarea>

    </div>
 </div>

  <div class="text-center">
    <button type="button" id="id_SubmitForm" class="btn btn-success">Submit</button>
  </div>
</form>

My JavaScript code is:

/* Latest compiled and minified JavaScript included as External Resource */
var question6AnsArray = [];

/* To hide all the text areas when the page loads */
$("#id_Question6_textinput").hide();
$("#id_q6_opH_textarea").hide();
$('#id_que6a').hide();
$('#id_que6b').hide();
$('#id_que6c').hide();
$('#id_que6d').hide();


 /* To show appropriate text area for selected check box */
 $('#input_que6a').click(function() {
     $("#id_que6a").fadeToggle(this.checked);
 });
 $('#input_que6b').click(function() {
     $("#id_que6b").fadeToggle(this.checked);
 });
 $('#input_que6c').click(function() {
     $("#id_que6c").fadeToggle(this.checked);
 });
 $('#input_que6d').click(function() {
    $("#id_que6d").fadeToggle(this.checked);
 });

 $('#other_que6').click(function() {
    $("#id_Question6_textinput").fadeToggle(this.checked);
 });

$("#id_SubmitForm").click(function() {

  // To get all the selected checkbox values and their associated textareas
  $('input[name="question6"]:checked').each(function() {
    question6AnsArray.push(this.value);

  //Here I want to get the value for the textarea.
  });
    var question6Answer = question6AnsArray.join(", ");
    alert(question6Answer);
});

Here is a link to JSFiddle

from your this context step back up to your parent then grab the next text element.

$('input[type="checkbox"]').on('click', function() {
    if ( $(this, ':checked') ) {
      $(this).val();
      $(this).parent().next('textarea').val();
    )
}
$("#id_SubmitForm").click(function() {

  // To get all the selected checkbox values and their associated textareas
  $('input[name="question6"]:checked').each(function() {
    question6AnsArray.push(this.value);
    question6AnsOtherArray.push($(this).closest("label").next("textarea").val());
  });
  var question6Answer = question6AnsArray.join(", ");
  var question6OtherAnswer = question6AnsOtherArray.join(", ");
  alert(question6Answer);
  alert(question6OtherAnswer);
});

And, You could actually optimize your code this way a little bit too..

/* Latest compiled and minified JavaScript included as External Resource */
var question6AnsArray = [];
var question6AnsOtherArray = [];

/* To hide all the text areas when the page loads */
$("#id_Question6_textinput").hide();
$("#id_MyForm textarea").hide();


/* To show appropriate text area for selected check box */

$("input[type=checkbox]").click(function() {
  $(this).closest("label").next("textarea").fadeToggle();
})

$('#other_que6').click(function() {
  $("#id_Question6_textinput").fadeToggle(this.checked);
});

$("#id_SubmitForm").click(function() {

  // To get all the selected checkbox values and their associated textareas
  $('input[name="question6"]:checked').each(function() {
    question6AnsArray.push(this.value);
    question6AnsOtherArray.push($(this).closest("label").next("textarea").val());
  });
  var question6Answer = question6AnsArray.join(", ");
  var question6OtherAnswer = question6AnsOtherArray.join(", ");
  alert(question6Answer);
  alert(question6OtherAnswer);
});

Try below function at last:

$("#id_SubmitForm").click(function() {
  question6AnsArray = [];
  // To get all the selected checkbox values and their associated textareas
  $('input[name="question6"]:checked').each(function() {
    question6AnsArray.push(this.value);
    question6AnsArray.push($('.form-control').val());

  //Here I want to get the value for the textarea.
  });
    var question6Answer = question6AnsArray.join(", ");
    alert(question6Answer);
});

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