2014-11-24 14 views
1

我想获得的日期,时间和地点与一个按钮的点击各自领域...如何输入日期和位置与时间一起在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();"/> 
+0

2014-11-24 17:20:24

+2

你可以编辑q来放置代码吗?评论中无法阅读。 – deitch 2014-11-24 17:21:52

+0

它只是显示在上面...我是否需要发布JavaScript以及? – 2014-11-24 17:27:21

回答

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浏览器),但我不一定会指望它。

+0

你是从哪里得到theDate ?? ..从他的HTML – 2014-11-24 17:39:28

+1

@AvinashBabu。 – canon 2014-11-24 17:40:04

+0

他的HTML可是没有theDate – 2014-11-24 17:41:18

相关问题