与jquery比较2次
预先感谢您的任何帮助...
我正在尝试(1)生成表单的开始时间和结束时间,(2)找到两者之间的差异,以及(3)将差异添加到新输入。
这是我到目前为止所做的:
<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”。
另外,这可能有助于了解点击链接之间的时间应该在 30 秒左右...
非常感谢你们的帮助,你们已经回答了我的很多问题,而无需我发帖!
Thanks in advance for any help...
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>
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".
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
parseInt()
次返回,否则它们只是字符串(由.val()
返回)。就我个人而言,我宁愿自己存储
begintime
和endtime
值,而不是存储在文本输入中(为什么用户需要看到它们?)。像这样:顺便说一句,我建议将 jQuery 代码从内联
标记中移出并移至外部 JS 文件中。这使得标记和 JS 更易于维护。只需将所有 JS 代码包装在文档就绪处理程序中即可:
或者更简洁地说,
You need to
parseInt()
the times back out, otherwise they're just strings (as returned by.val()
).Personally, I'd sooner just store the
begintime
andendtime
values myself, rather than in text inputs (why does the user need to see them, anyway?). Like this: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:or, more concisely,