简体   繁体   English

Javascript复选框填充输入并将不同的值传递给表单

[英]Javascript checkboxes fill input AND pass a different value to a form

For a game, I have crimes listed where there is basically the crimes, then two checkboxes. 对于游戏,我列出了一些罪行,其中基本上是罪行,然后是两个复选框。 One checkbox is for if the crime was committed, the other checkbox tells if the crime was aggravated or not. 一个复选框用于确定犯罪是否发生,另一个复选框用于确定犯罪是否被加重了。 In the form, there is a field that says the total number of minutes the person will serve in jail. 表格中有一个字段,说明该人将在监狱服刑的总分钟数。 BUT, I also need to pass the class of that crime to the form so that when it's submitted it lists the class of the crime. 但是,我还需要将该罪行的类别传递到表单,以便在提交该罪行时列出该罪行的类别。 Here's what I have so far. 到目前为止,这就是我所拥有的。

The Markup: 标记:

<form action="process.php" method="post">
    Charges <input name="charges" type="text" id="charges" value=""><br />
    <input name="time" type="text" id="time" value=""> minutes<br />
    <input name="id" type="hidden" id="id" value="">
    <input type="submit" value="Process" />
</form>

<table>
    <tr>
        <td><input type="checkbox" value="15" id="a3a" class="sum" data-toggle="checkbox" /></td>
        <td><input type="checkbox" value='60' id="c3a" class="sum" data-toggle="checkbox" /></td>
        <td>Battery</td>
    </tr>
    <tr>
        <td><input type="checkbox" value="0" id="a3b" class="sum" data-toggle="checkbox" /></td>
        <td><input type="checkbox" value='45' id="c3b" class="sum" data-toggle="checkbox" /></td>
        <td>Bank Robbery</td>
    </tr>
    <tr>
        <td><input type="checkbox" value="10" id="a3c" class="sum" data-toggle="checkbox" /></td>
        <td><input type="checkbox" value='30' id="c3c" class="sum" data-toggle="checkbox" /></td>
        <td>Robbery</td>
    </tr>
    <tr>
        <td><input type="checkbox" value="15" id="a3d" class="sum" data-toggle="checkbox" /></td>
        <td><input type="checkbox" value='60' id="c3d" class="sum" data-toggle="checkbox" /></td>
        <td>Kidnapping</td>
    </tr>
</table>

Here is the Javascript: 这是Javascript:

<script>
$(document).ready(function() {
    function updateSum() {
      var total = 0;
      $(".sum:checked").each(function(i, n) {total += parseInt($(n).val());})
      if(total<201){
        $("#time").val(total);
      }else{
        $("#time").val(200);
      }

    }
    // run the update on every checkbox change and on startup
    $("input.sum").change(updateSum);
    updateSum();    
})
</script>

So all good there, if you check a box it adds the total time to the time field up to 200 minutes (that's the max you can serve in game). 因此,一切都很好,如果您选中一个复选框,它将在时间字段中增加总时间(最多200分钟)(这是您可以在游戏中使用的最大时间)。 HOWEVER, I also want to add the crime to the charges field. 但是,我也想将罪行添加到“收费”字段中。 IE, if you check the battery checkbox (#c3a) it adds "Battery" to the charges field, if you check the aggravated box (#a3a) then it will add "Aggravated Battery" instead. IE,如果您选中电池复选框(#c3a),则会在“费用”字段中添加“电池”,如果选中加重框(#a3a),则会添加“加重电池”。 For each thing you check it will add that charge to the charges field (comma delimited) and then also for everything you check it will add the class (which currently sits in the ID slot) to the hidden id field. 对于您检查的每件事,它都会将该费用添加到“收费”字段(以逗号分隔),然后对于您检查的每件事,还将其类别(当前位于ID插槽中)添加到隐藏的ID字段。 I figured out how to tally the time, but how do I send 3 different variables to 3 different inputs fields using a single checkbox pair? 我想出了如何计算时间,但是如何使用一个复选框对将3个不同的变量发送到3个不同的输入字段?

BTW, these crime lists are not static, the html is actually written out using php from a database, that's why I was using a recursive javascript that went through all the checkboxes on the page rather than specific IDs. 顺便说一句,这些犯罪列表不是一成不变的,实际上是使用php从数据库中写出了html,这就是为什么我使用了遍历页面上所有复选框而不是特定ID的递归javascript的原因。

Assuming you need to save the crime names from each row if a checkbox is checked, you can use the hidden input field in the form for "charges" and each time a box is checked or unchecked, modify the value of the hidden field to only include the selected crimes. 假设如果选中一个复选框,则需要保存每一行的犯罪名称,则可以将表单中的隐藏输入字段用于“费用”,并且每次选中或取消选中一个框时,将隐藏字段的值修改为仅包括选定的犯罪。

HTML: HTML:

<form action="process.php" method="post">
  Charges
  <input name="charges" type="text" id="charges" value="">
  <br />
  <input name="time" type="text" id="time" value=""> minutes
  <br />
  <input name="id" type="hidden" id="id" value="" />
  <input type="submit" value="Process" />
</form>

<table>
  <tr>
    <td>
      <input type="checkbox" value="15" id="a3a" class="sum" data-toggle="checkbox" />
    </td>
    <td>
      <input type="checkbox" value='60' id="c3a" class="sum" data-toggle="checkbox" />
    </td>
    <td>Battery</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" value="0" id="a3b" class="sum" data-toggle="checkbox" />
    </td>
    <td>
      <input type="checkbox" value='45' id="c3b" class="sum" data-toggle="checkbox" />
    </td>
    <td>Bank Robbery</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" value="10" id="a3c" class="sum" data-toggle="checkbox" />
    </td>
    <td>
      <input type="checkbox" value='30' id="c3c" class="sum" data-toggle="checkbox" />
    </td>
    <td>Robbery</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" value="15" id="a3d" class="sum" data-toggle="checkbox" />
    </td>
    <td>
      <input type="checkbox" value='60' id="c3d" class="sum" data-toggle="checkbox" />
    </td>
    <td>Kidnapping</td>
  </tr>
</table>

JavaScript: JavaScript:

$(document).ready(function() {
  function updateSum() {
    var total = 0,
      crimes = [];
    $(".sum:checked").each(function(i, n) {
      total += parseInt($(n).val());

      // declare variables for DOM objects and name of crime
      var $cb = $(this),
        $row = $cb.parents('tr'),
        name = $row.find('td').last().text();

            // check that the crimes array doesn't already have the value; add if necessary
      if (crimes.filter(function(c, i, a) {
          return c == name;
        }).length == 0) {
        crimes.push(name);
      }
    });

    if (total < 201) {
      $("#time").val(total);
    } else {
      $("#time").val(200);
    }
    $('#charges').val(crimes.join(','));
  }

  // run the update on every checkbox change and on startup
  $("input.sum").change(updateSum);
  updateSum();
})

Live Example 现场例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM