与jquery比较2次 [英] Comparing 2 times with jquery

查看:129
本文介绍了与jquery比较2次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢您的帮助...

Thanks in advance for any help...

我试图(1)生成表单的开始时间和结束时间,(2)找到两者之间的差异,(3)将差异添加到新输入中.

I'm trying to (1) generate a begin time and end time for a form, (2) find the difference between the two, and (3) add the difference to a new input.

这是我到目前为止所拥有的:

Here's what I have so far:

<a href="#" id="starttime">Begin time</a>
<input id="starttimeinput" name="starttimeinput" type="text" value="">
<script>
    $("#starttime").click(function () {
         var begintime = event.timeStamp;
         $("#starttimeinput").val(begintime);
    });
</script>
<a href="#" id="endtime">end time</a>
<input id="endtimeinput" name="endtimeinput" type="text" value="">
<script>
    $("#endtime").click(function () {
         var endtime = event.timeStamp;
         $("#endtimeinput").val(endtime);
    });
</script>
<input id="totaltime" name="totaltime" type="text">
<script>
    $("#totaltime").focus(function () {
       var begintime = $("#starttimeinput").val();
       var endtime = $("#endtimeinput").val();
       var totaltime = endtime - begintime;
       $("#totaltime").val(totaltime); 
     });     
</script>

第一部分起作用(将时间戳输入到开始时间和结束时间输入中).我以前从未使用过数字,也无法弄清楚第二部分.出现的结果是"NaN".

The first part works (entering the timestamps into the beginning time and end time inputs). I've never worked with numbers before and can't figure out the second part. The result that comes up is "NaN".

这也可能有助于了解单击链接之间的时间应该在30秒左右...

Also this might be useful to know the the time between when the links are clicked should be around 30 seconds...

非常感谢你们在没有我发布的情况下回答了我很多问题!

Thanks much for any help you guys have answered so many questions of mine without me having to post!

推荐答案

您需要 parseInt() 超时,否则它们只是字符串(由.val()返回).

$("#totaltime").focus(function () {
    var begintime = parseInt($("#starttimeinput").val(), 10),
        endtime = parseInt($("#endtimeinput").val(), 10),
        totaltime = endtime - begintime;
    $("#totaltime").val(totaltime); 
 });


就我个人而言,我将自己直接存储begintimeendtime值,而不是在文本输入中存储(为什么用户仍然需要查看它们?).像这样:


Personally, I'd sooner just store the begintime and endtime values myself, rather than in text inputs (why does the user need to see them, anyway?). Like this:

var begintime,
    endtime;
$("#starttime").click(function (event) {
     begintime = event.timeStamp;
     //$("#starttimeinput").val(begintime);
});

$("#endtime").click(function (event) {
     endtime = event.timeStamp;
     //$("#endtimeinput").val(endtime);
});

$("#totaltime").focus(function () {
    $("#totaltime").val(endtime - begintime); 
});


另一方面,我建议将jQuery代码从内联<script>标记中移出,并移至外部JS文件中.这使得标记和JS更具可维护性.只需将所有JS代码包装在文档就绪处理程序中即可:


On a side note, I would recommend moving your jQuery code out of inline <script> tags and into an external JS file. This makes for more maintainable markup and JS. Just wrap all of your JS code in a document ready handler:

$(document).ready(function () {
    /* your code here */
});

或更简而言之,

$(function () {
    /* your code here */
});

这篇关于与jquery比较2次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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