【问题标题】:How to separate my event handling from HTML code如何将我的事件处理与 HTML 代码分开
【发布时间】:2016-08-28 06:01:15
【问题描述】:

我的代码工作正常,但有没有办法做到这一点,而无需在 HTML 表单中包含我的事件(onclick)。我该如何使用事件监听器?

这里是 HTML:

<strong>Select Your fees Options</strong></label><br/>
<input type="checkbox" name="fee" id="exc" value="12000.00" onclick = "updateFee()"  required><label>PTA and Excursion and Magazine Fee N12,000.00</label><br>
<input type="checkbox" name="fee"  id="fol" value="3000.00" onclick = "updateFee()" ><label>Folder Fee <b>(optional)</b> N3,000.00</label><br>
<input type="checkbox" name="fee"  id="ext"    value="15000.00"  onclick = "updateFee()" ><label>Extra Lesson <b>(optional)</b> N15,000.00</label><br>
<input type="checkbox" name="fee"  id="clu" value="15000.00"  onclick = "updateFee()"><label>Club <b>(optional)</b> N15,000.00</label><br>
<!--total fee is here-->   
<p><b>Total Fee:
<input  name="amt" id="amt" class="form-control number" type="text" /> <br>

这里是 JavaScript:

<script> 
function updateFee(){
var  totFee = 0;
var fee=document.getElementsByName("fee");
for (i = 0; i < fee.length; i++)
{
if (fee[i].checked == true)
{
totFee += parseInt(fee[i].value, 10);       
}
}
amount = totFee  ;
document.getElementById("amt").value = amount;
}

谢谢!

【问题讨论】:

  • 请正确缩进您的代码。顺便说一句,您不需要将布尔值与true 进行比较;写if (fee[i].checked).
  • 缩进完成!谢谢你的纠正。 @torazaburo

标签: javascript html arrays


【解决方案1】:

使用javascript/jQuery

在你的情况下

<strong>Select Your fees Options</strong></label><br/>
<input type="checkbox" name="fee" id="exc" value="12000.00" required><label>PTA and Excursion and Magazine Fee N12,000.00</label><br>
<input type="checkbox" name="fee"  id="fol" value="3000.00"><label>Folder Fee <b>(optional)</b> N3,000.00</label><br>
<input type="checkbox" name="fee"  id="ext"    value="15000.00"><label>Extra Lesson <b>(optional)</b> N15,000.00</label><br>
<input type="checkbox" name="fee"  id="clu" value="15000.00"><label>Club <b>(optional)</b> N15,000.00</label><br>
<!--total fee is here-->   
<p><b>Total Fee:
<input  name="amt" id="amt" class="form-control number" type="text" /> <br>

然后,你的脚本:

使用 jQuery

$(document).ready(function() {
   $('input[name=fee]').on('click', function() {
      var fee = 0;
      $('input[name=fee]:checked').each(function() {
         fee+=parseFloat($(this).val());
      });
      $('#amt').val(fee);
   });
});

或者,不使用 jQuery(使用您的 updateFee 函数:

// run this script after page finish loading
window.onload = function() {
    // get all the checkboxes you want to listen to
    var cbs = document.getElementsByName('fee');

    // attach click event for each one
    for(var i=0; i<cbs.length; i++) {
       cbs[i].onclick=updateFee;
    }
};

【讨论】:

  • 哇!那很快。我更喜欢在 javascript 中使用它,因为在跳入 jquery 之前我仍在尝试掌握 javascript。谢谢。 @Yaron U.
  • 如果您想将多个事件绑定到单个控件上的单个事件,您可以使用addEventListener
  • @AbdulhakeemImran 你永远不需要跳进 jQuery,你也不应该,除非你喜欢回到过去。
  • @AbdulhakeemImran 首先,您没有将updateFee 函数添加到您的小提琴中。其次,JSFiddle 在 window.onload 回调中运行脚本 - 所以为了看到它在 JSFiddle 中工作,您需要在没有第一行和最后一行的情况下运行它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 2019-01-04
  • 1970-01-01
  • 2018-01-25
  • 2016-08-04
相关资源
最近更新 更多