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

时间:2014-11-24 17:18:59

标签: 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 个答案:

答案 0 :(得分:2)

两个问题。

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()找到它。

<input type="button" value="Get Location / Time" 
    onclick="getLocationConstant(1);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指定的值应符合以下规则(source):

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

id中有空格。有些浏览器可能允许(例如Chrome),但我不一定会依赖它。

相关问题