从选中范围的单选添加数字值,然后从单独的复选框添加/删除数字值

时间:2019-06-22 10:54:14

标签: javascript html checkbox radio

我有很多可用无线电选择的数量,我希望这些价格出现在TotalPrice跨度中。但是,我还希望在运输数量的下面还有一个复选框,我希望将运输选项添加到数量价格之上的TotalPrice范围内。

<tr style="text-align: center;">
  <th scope="row">
    <input
      class="form-check-input"
      type="radio"
      name="quantity"
      id="quantity1"
      value="1"
    />1
  </th>
  <td>FREE + $6.97 S&H</td>
</tr>
<tr style="text-align: center;">
  <th scope="row">
    <input
      class="form-check-input"
      type="radio"
      name="quantity"
      id="quantity2"
      value="2"
    />2
  </th>
  <td>FREE + $9.97 S&H</td>
</tr>
<tr style="text-align: center;">
  <th scope="row">
    <input
      class="form-check-input"
      type="radio"
      name="quantity"
      id="quantity4"
      value="4"
    />4
  </th>
  <td>FREE + $16.97 S&H</td>
</tr>
<tr style="text-align: center;">
  <th scope="row">
    <input
      class="form-check-input"
      type="radio"
      name="quantity"
      id="quantity6"
      value="6"
    />6
  </th>
  <td>FREE + $21.97 S&H</td>
</tr>
<tr style="text-align: center;">
  <th scope="row">
    <input
      class="form-check-input"
      type="radio"
      name="quantity"
      id="quantity8"
      value="8"
    />8
  </th>
  <td>FREE + $26.97 S&H</td>

  <input
    class="form-check-input"
    type="checkbox"
    name="ship"
    value="Expedited"
    id="exp1"
  />

  <span id="TotalPrice"></span>
</tr>

还有,是否有可能通过php中的$ _POST传递值(数量,加急和TotalPrice)?

1 个答案:

答案 0 :(得分:0)

要更新跨度中的数量,我建议使用一个函数,该函数获取按钮的“ checked”属性并返回在跨度中将其打印出来的相应值,例如

function updateValue() {
    var qty=0
    var elems=document.getElementsByClassName("form-check-input");
    for(var i=0; i<elems.length; i++) {
        if(elems[i].checked) {
            qty=elems[i].value //put price in value before
            break //got the one we want, stop searching
        }
    }
    var chkbox=document.getElementById("exp1")
    var chkstr=chkbox.checked?chkbox.value:""
    document.getElementById("totalPrice").innerHTML=qty+chkstr
}

要将其发送到PHP,请将其全部放入action="/path/to/php"method="post"的形式;它将把任何值发送到PHP中的$ _POST变量(带有您代码的$ _POST [“ quantity”]中的数量)。