如何在JavaScript中输入日期和时间以及位置

how to input date and time along with location in javascript

本文关键字:时间 位置 日期 输入 JavaScript      更新时间:2023-09-26

我正在尝试通过单击一个按钮将日期、时间和位置放入各自的字段中......

这是我正在使用的代码...

  <label>Latitude:</label> <input type="text" id="latitude1" name="Latitude1" value=""       readonly />
    <label>Longitude:</label> <input type="text" id="longitude1" name="Longitude1" value="" readonly />
     <label>Date / Time:</label> <input type="text" id="Time Check In"  size="50" class="field left" readonly/>
     <input type="button" value="Get Location / Time" onclick="getLocationConstant(1)"; onclick="this.form.theDate.value = new Date();"/> 

两个问题。

1:您已经指定了两次onclick。那行不通。

<input type="button" value="Get Location / Time" onclick="getLocationConstant(1)"; 
onclick="this.form.theDate.value = new Date();"/>

2: theDate不存在...

<input type="button" value="Get Location / Time" onclick="getLocationConstant(1)"; 
onclick="this.form.theDate.value = new Date();"/>

您的输入 ID Time Check In ;使用 document.getElementById() 查找它。

<输入类型 值="获取位置/时间" onclick="getLocationConstant(1);<b">document.getElementById('Time Check In').value = new Date();"/>

下面是一个演示:

function getLocationConstant(){/* your location stuff */}
<label>Latitude:</label>
<input type="text" id="latitude1" name="Latitude1" value="" readonly />
<label>Longitude:</label>
<input type="text" id="longitude1" name="Longitude1" value="" readonly />
<label>Date / Time:</label>
<input type="text" id="Time Check In" size="50" class="field left" readonly />
<input type="button" value="Get Location / Time" onclick="getLocationConstant(1);document.getElementById('Time Check In').value = new Date();"/> 

请注意,为id指定的值应遵循以下规则(来源):

  • 长度必须至少为一个字符
  • 不得包含任何空格字符

你的id有空间。有些浏览器可能允许这样做(例如,Chrome允许),但我不一定指望它。