我试图通过单击一个按钮将日期、时间和位置获取到各自的字段中...
这是我正在使用的代码...
<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" <b>onclick</b>="getLocationConstant(1)";
<b>onclick</b>="this.form.theDate.value = new Date();"/>
2: theDate
不存在...
<input type="button" value="Get Location / Time" onclick="getLocationConstant(1)";
onclick="this.form.<b>theDate</b>.value = new Date();"/>
您输入的 ID 是 Time Check In
;使用 document.<a href="https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById" rel="noreferrer noopener nofollow">getElementById()</a>
找到它。
<input type="button" value="Get Location / Time" onclick="getLocationConstant(1);document.getElementById('Time Check In').value = new Date();" />
Here's a demo:
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
指定的值应遵守以下规则 ( source ):
- must be at least one character long
- must not contain any space characters
您的 id
中有空格.某些浏览器可能允许这样做(例如 Chrome 允许这样做),但我不一定指望它。
关于javascript - 如何在javascript中输入日期和时间以及位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27110430/