如何使JQuery在HTA应用程序中工作 [英] How to make JQuery work in HTA application

查看:143
本文介绍了如何使JQuery在HTA应用程序中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的网络应用程序转换为HTA应用程序。

I want to transform my web application into an HTA application.

这是我的整个 HTA文件

<!DOCTYPE html>
<html lang="en-US">
<HTA:APPLICATION applicationname="Test" border="no" caption="yes" showintaskbar="no" singleinstance="yes" sysmenu="yes" selection="no" innerborder="no" scroll="yes"/>



<head>
<title>Test</title>
<meta http-equiv="x-ua-compatible" content="ie=11"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">

var numRows = 2,
  ti = 5;
var tableCount = 1;
var index = 1;

window.standBy = function() {
  var sum = 0;
  $(".Standby").each(function(index, stand) {
    sum += parseFloat($(stand).val());
  })

  $(".grandtotal").val(sum)
}

function calculate() {
  var tr = $(this).closest('tr');

  var hours = parseInt($(".Time2", tr).val().split(':')[0], 10) - parseInt($(".Time1", tr).val().split(':')[0], 10);
  if (hours < 0) hours = 24 + hours;
  $(".Hours", tr).val(hours);

  if (hours <= 4) $(".Standby", tr).val("0.5");
  if (hours == 4) $(".Standby", tr).val("0.5");
  if (hours > 4 && hours < 8) $(".Standby", tr).val("1");
  if (hours == 8) $(".Standby", tr).val("1");
  if (hours > 8 && hours < 12) $(".Standby", tr).val("1.5");
  if (hours == 12) $(".Standby", tr).val("1.5");
  if (hours > 12 && hours < 16) $(".Standby", tr).val("2");
  if (hours == 16) $(".Standby", tr).val("2");
  if (hours > 16 && hours < 20) $(".Standby", tr).val("2.5");
  if (hours == 20) $(".Standby", tr).val("2.5");
  if (hours > 20 && hours < 24) $(".Standby", tr).val("3");
  if (hours == 24) $(".Standby", tr).val("3");
  if (hours > 24) alert("You cannot exceed a 24 hour period.");



}
$('#table').on('change', ".Time1,.Time2", calculate);
$('#table').find(".Time1").trigger('change')


window.addTime = function() {
  tableCount++;
  $('#timeTable').clone().attr('id', "timeTable" + tableCount).appendTo('#table');
  $('#timeTable' + tableCount).find("input").val("");
  index++;
  $('#timeTable' + tableCount + ' .increment').html(tableCount);

};


$(document).on('click', 'button.removeTime', function() {
  var closestTable = $(this).closest('table');
  if (closestTable.attr('id') != "timeTable") {
    closestTable.remove();
  }
  tableCount--;
  if (tableCount < 1) {
    tableCount = 1;
  }

  standBy();
  return false;
});
</script>
</head>



<body>

<h1 class="text-center">Compensatory Time for Standby Hours Calculator</h1>
<br>
<br>
<br>

<h3>
Time format is in 24h
</h3>
<h6><I>Example: If you want to type in "8 AM", the correct format would be: "8". <br> If you want to type in "8 PM", the correct format would be "20".</I></h6>

<div id="table">
  <table id="timeTable" class="tg table table-striped table-bordered table-condensed tab_logic">
    <tr class="headings">
      <th class="tg-yw41"></th>
      <th class="tg-yw41"></th>
      <th class="tg-yw4l">Standby Start Time</th>
      <th class="tg-yw4l">Standby End Time</th>
      <th class="tg-yw4l">Hours in total</th>
      <th class="tg-yw4l">Compensatory Hours Earned</th>
    </tr>
    <tr>
      <td class="increment headings">1</td>
      <td class="tg-yw4l headings">
        <button class="removeTime btn btn-danger">Remove Time</button>
      </td>

      <td class="tg-yw4l headings">
        <input class="Time1 form-control input-md " value="" placeholder="Enter your start time" />
      </td>
      <td class="tg-yw4l headings">
        <input class="Time2 form-control input-md" value="" placeholder="Enter your end time" />
      </td>
      <td class="tg-yw4l headings">
        <input type="text" class="Hours form-control input-md" value="0" readonly="" />
      </td>
      <td class="tg-yw4l headings">
        <input type="text" class="Standby form-control input-md" value="0" readonly="" />
      </td>
    </tr>
  </table>
</div>







<hr>

<button class="btn btn-success pull-left" onclick="addTime();"><span class="glyphicon glyphicon-plus-sign">Add Time</span></button>
<br>
<br>

<button class="btn btn-primary" onclick="standBy();">Calculate Total Compensatory Hours Earned</button>

<caption>Total Compensatory Hours:</caption>&nbsp;
<input class="grandtotal" value="" readonly="" />



</body>

</html>

我还有一个工作的我的Web应用程序的JSFiddle,我想转换成HTA应用程序:< a href =https://jsfiddle.net/32u1vuoc/ =nofollow noreferrer> https://jsfiddle.net/32u1vuoc/

I also have a working JSFiddle of my web application that I want to transform into an HTA application: https://jsfiddle.net/32u1vuoc/

HTA文件中JQuery的整个功能完美无缺,唯一不起作用的是计算小时数。这意味着每当我进入待机开始时间和待机结束时间时,总时数和获得的补偿时间显示为0。它没有计算任何东西。

The whole functionality of the JQuery in the HTA file works perfectly, the only thing that doesn't work is the calculation of hours. Which means that whenever I enter the Standby Start Time and Standby End Time the Hours in total and Compensatory Hours Earned displays "0". It doesn't calculate anything.

我想知道这个社区中是否有人知道这个问题的解决方案。

I was wondering if any of you in this community would know a solution to this.

感谢您的时间!

推荐答案

您需要在身体之前添加脚本,这就是为什么它的原因不工作。

You need to add the scripts after the body not before, that's why it's not working.

        <!DOCTYPE html>
    <html lang="en-US">
    <HTA:APPLICATION applicationname="Test" border="no" caption="yes" showintaskbar="no" singleinstance="yes" sysmenu="yes" selection="no" innerborder="no" scroll="yes"/>



    <head>
    <title>Test</title>
    <meta http-equiv="x-ua-compatible" content="ie=11"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    </head>



    <body>

    <h1 class="text-center">Compensatory Time for Standby Hours Calculator</h1>
    <br>
    <br>
    <br>

    <h3>
    Time format is in 24h
    </h3>
    <h6><I>Example: If you want to type in "8 AM", the correct format would be: "8". <br> If you want to type in "8 PM", the correct format would be "20".</I></h6>

    <div id="table">
      <table id="timeTable" class="tg table table-striped table-bordered table-condensed tab_logic">
        <tr class="headings">
          <th class="tg-yw41"></th>
          <th class="tg-yw41"></th>
          <th class="tg-yw4l">Standby Start Time</th>
          <th class="tg-yw4l">Standby End Time</th>
          <th class="tg-yw4l">Hours in total</th>
          <th class="tg-yw4l">Compensatory Hours Earned</th>
        </tr>
        <tr>
          <td class="increment headings">1</td>
          <td class="tg-yw4l headings">
            <button class="removeTime btn btn-danger">Remove Time</button>
          </td>

          <td class="tg-yw4l headings">
            <input class="Time1 form-control input-md " value="" placeholder="Enter your start time" />
          </td>
          <td class="tg-yw4l headings">
            <input class="Time2 form-control input-md" value="" placeholder="Enter your end time" />
          </td>
          <td class="tg-yw4l headings">
            <input type="text" class="Hours form-control input-md" value="0" readonly="" />
          </td>
          <td class="tg-yw4l headings">
            <input type="text" class="Standby form-control input-md" value="0" readonly="" />
          </td>
        </tr>
      </table>
    </div>







    <hr>

    <button class="btn btn-success pull-left" onclick="addTime();"><span class="glyphicon glyphicon-plus-sign">Add Time</span></button>
    <br>
    <br>

    <button class="btn btn-primary" onclick="standBy();">Calculate Total Compensatory Hours Earned</button>

    <caption>Total Compensatory Hours:</caption>&nbsp;
    <input class="grandtotal" value="" readonly="" />



    </body>
    <script type="text/javascript">

    var numRows = 2,
      ti = 5;
    var tableCount = 1;
    var index = 1;

    window.standBy = function() {
      var sum = 0;
      $(".Standby").each(function(index, stand) {
        sum += parseFloat($(stand).val());
      })

      $(".grandtotal").val(sum)
    }

    function calculate() {
      var tr = $(this).closest('tr');

      var hours = parseInt($(".Time2", tr).val().split(':')[0], 10) - parseInt($(".Time1", tr).val().split(':')[0], 10);
      if (hours < 0) hours = 24 + hours;
      $(".Hours", tr).val(hours);

      if (hours <= 4) $(".Standby", tr).val("0.5");
      if (hours == 4) $(".Standby", tr).val("0.5");
      if (hours > 4 && hours < 8) $(".Standby", tr).val("1");
      if (hours == 8) $(".Standby", tr).val("1");
      if (hours > 8 && hours < 12) $(".Standby", tr).val("1.5");
      if (hours == 12) $(".Standby", tr).val("1.5");
      if (hours > 12 && hours < 16) $(".Standby", tr).val("2");
      if (hours == 16) $(".Standby", tr).val("2");
      if (hours > 16 && hours < 20) $(".Standby", tr).val("2.5");
      if (hours == 20) $(".Standby", tr).val("2.5");
      if (hours > 20 && hours < 24) $(".Standby", tr).val("3");
      if (hours == 24) $(".Standby", tr).val("3");
      if (hours > 24) alert("You cannot exceed a 24 hour period.");



    }
    $('#table').on('change', ".Time1,.Time2", calculate);
    $('#table').find(".Time1").trigger('change')


    window.addTime = function() {
      tableCount++;
      $('#timeTable').clone().attr('id', "timeTable" + tableCount).appendTo('#table');
      $('#timeTable' + tableCount).find("input").val("");
      index++;
      $('#timeTable' + tableCount + ' .increment').html(tableCount);

    };


    $(document).on('click', 'button.removeTime', function() {
      var closestTable = $(this).closest('table');
      if (closestTable.attr('id') != "timeTable") {
        closestTable.remove();
      }
      tableCount--;
      if (tableCount < 1) {
        tableCount = 1;
      }

      standBy();
      return false;
    });
    </script>

    </html>

或者在附加事件处理程序时使用文档就绪,因为现在您尝试将事件添加到元素在DOM中尚不存在。

Or use on document ready when you attach your event handlers because now you try to add the event to elements that don't exist yet in the DOM.

$(document).ready(function(){
  $('#table').on('change', ".Time1,.Time2", calculate);
  $('#table').find(".Time1").trigger('change')

});

这篇关于如何使JQuery在HTA应用程序中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆