添加和删​​除输入复选框的值

时间:2018-06-28 10:07:30

标签: javascript jquery html

我编写的脚本(必须有帮助)允许用户选择复选框,并将选定框的值添加到输入中,并用逗号分隔。

$(document).ready(function() {
  var variable = 1
  $("#add").click(function() {
    var element = $(".test.hidden").clone(true);
    var testInputs = element.find(".testInputs").click(clickHandler);


    testInputs.attr("unique-index", variable);
    var target = element.find('.user_input2 #insert');
    target.attr("unique-identifier", variable)
    element.removeClass("hidden").appendTo(".paste:last");
    variable += 1;

  });

  function clickHandler() {
    console.log($(this).attr('unique-index'));
    const index = $(this).attr('unique-index')
    const targetsValue = $(`#insert[unique-identifier="${index}"]`).val();
    console.log('sup', targetsValue)
    const combo = targetsValue ? `${targetsValue}, ${$(this).val()}` : $(this).val()
    $(`#insert[unique-identifier="${index}"]`).attr('value', combo);
  }


  $(".test").keyup(function() {
    var test;
    console.log('sup')
    if ($(".test").is(":checked")) {

      test = $(".test").val();
    };

    $("#insert").val(test);

  });

  $("#testbtn").click(function() {
    $(".test").keyup();
  });

});
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Test</h2>

<button id="add">Add</button>

<div class="test hidden">

  <div class="user_input2">
    <table>
      <thead>
        <tr>
          <th>Pick Option</th>
        </tr>
      </thead>
      <tbody>
        <tr id="append">
          <td><input class="testInputs" name="test" value="1" type="checkbox">Option1</td>
          <td><input class="testInputs" name="test" value="2" type="checkbox">Option2</td>
          <td><input class="testInputs" name="test" value="3" type="checkbox">Option3</td>
        </tr>
      </tbody>
    </table>
    <input type="text" id="insert" name="check[]">
    <button class="hidden" id="testbtn">Calc</button>
  </div>
</div>



<div class="paste">
</div>

取消选中此框后,我似乎无法消除增加的价值。我需要识别选中的框,然后根据选中的状态删除该值。如果有人可以提供帮助,我将非常感激。

P.s。它还应不允许重复值,如多次按下输入检查时所见。

1 个答案:

答案 0 :(得分:0)

您应该在更改事件中获得checked复选框的值

//Added event listeners to element
$(document).on('change', '.testInputs', function () {
    //Target common ansestor
    var testContainer = $(this).closest('.test');
    //get checked checkboxes value in an array
    var checked = testContainer.find(':checked').map(function () {
            return this.value;
        }).get();

    //Target the textbox and set its value
    testContainer.find(".insert").val(checked.join(','));
});

此外,由于HTML中的标识符必须唯一,因此您应该使用CSS类

<input type="text" class="insert" name="check[]">

$(document).ready(function() {
  var variable = 1
  $("#add").click(function() {
    var element = $(".test.hidden").clone(true);
    var testInputs = element.find(".testInputs").click(clickHandler);


    testInputs.attr("unique-index", variable);
    var target = element.find('.user_input2 #insert');
    target.attr("unique-identifier", variable)
    element.removeClass("hidden").appendTo(".paste:last");
    variable += 1;

  });

  function clickHandler() {
    const index = $(this).attr('unique-index')
    const targetsValue = $(`#insert[unique-identifier="${index}"]`).val();
    const combo = targetsValue ? `${targetsValue}, ${$(this).val()}` : $(this).val()
    $(`#insert[unique-identifier="${index}"]`).attr('value', combo);
  }
  
  //Added event listeners to element
  $(document).on('change', '.testInputs', function() {
    //Target common ansestor
    var testContainer = $(this).closest('.test');
    //get checked checkboxes value in an array
    var checked = testContainer.find(':checked').map(function() {
      return this.value;
    }).get();

    //Target the textbox and set its value
    testContainer.find(".insert").val(checked.join(','));
  });
});
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Test</h2>

<button id="add">Add</button>

<div class="test hidden">

  <div class="user_input2">
    <table>
      <thead>
        <tr>
          <th>Pick Option</th>
        </tr>
      </thead>
      <tbody>
        <tr id="append">
          <td><input class="testInputs" name="test" value="1" type="checkbox">Option1</td>
          <td><input class="testInputs" name="test" value="2" type="checkbox">Option2</td>
          <td><input class="testInputs" name="test" value="3" type="checkbox">Option3</td>
        </tr>
      </tbody>
    </table>
    <input type="text" class="insert" name="check[]">
    <button class="hidden" id="testbtn">Calc</button>
  </div>
</div>

<div class="paste">
</div>