在Javascript中获取所选复选框及其相关textareas的值

时间:2015-12-30 05:07:16

标签: javascript jquery html checkbox

我有一个HTML表单。在这个表单中,我有5个复选框,每个复选框都有一个关联的textarea。 所以我想获取仅选中复选框及其关联的textarea文本的值。我能够获取已选中复选框的值,但我无法找到获取其关联的textarea值的方法。

我的HTML代码是:

<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>

我的JavaScript代码是:

/* 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);
});

以下是JSFiddle

的链接

3 个答案:

答案 0 :(得分:1)

从此上下文返回到您的父级,然后获取下一个文本元素。

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

答案 1 :(得分:1)

$("#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);
});

而且,您实际上可以通过这种方式优化代码......

/* 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);
});

答案 2 :(得分:0)

最后尝试以下功能:

$("#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);
});