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

标签 javascript

我试图通过单击一个按钮将日期、时间和位置获取到各自的字段中...

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

  <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/

相关文章:

javascript - 为什么选择选项更改事件不能有 this 和 event.target 来获取所选值?

javascript - React Native TextInput 多行数字键盘类型?

javascript - Angularjs - 如何 'ng-repeat' 'value in ng-model' 次 div?

javascript - js babel return wait 不起作用

javascript - jquery animate finish触发中途动画

javascript - 在不使用无序列表的情况下使用 CSS 显示子菜单

javascript - 页面路由在 Angular 8 中无法正常工作

javascript - 在空白行上获取内容可编辑的 UIWebView 中的插入符号

javascript - jquery:创建幻灯片轮播

java - 防止双重表单提交的最佳实践是什么