Javascript从输入字段填充var

时间:2014-01-21 09:29:47

标签: javascript html cordova

我有一些带有一些变量的输入字段和脚本。 我试图让输入字段填充变量,但我做错了,因为它没有为我做任何事情。

我尝试添加输入字段,如下所示:

<div data-role="content">
  <form type="post" onsubmit="calendarDemoAdd();">
    <div data-role="fieldcontain">
      <label for="title"> Reminder for </label>
      <input name="" id="title" placeholder="" value="" type="text">
    </div>
    <div data-role="fieldcontain">
      <label for="where"> Where </label>
      <input name="" id="where" placeholder="" value="" type="text">
    </div>
    <div data-role="fieldcontain">
      <label for="notes"> Notes </label>
      <input name="" id="notes" placeholder="" value="" type="text">
    </div>
    <div data-role="fieldcontain">
      <label for="startDate"> Start Date </label>
      <input name="" id="startDate" placeholder="September 24, 2014 14:30:00" value="" type="date">
    </div>
    <div data-role="fieldcontain">
      <label for="endDate"> End Date </label>
      <input name="" id="endDate" placeholder="September 24, 2014 14:30:00" value="" type="date">
    </div>
    <input type="submit" value="Submit" data-inline="true"/>
  </form>  // inserted missing tag
</div>

我也像这样编辑了javascript文件:

"use strict";

// create an event starting now, lasting an hour
var startDate = document.getElementById("startDate").value;
var endDate = document.getElementById("endDate").value;


var title = document.getElementById("title").value;
var where = document.getElementById("where").value;
var notes = document.getElementById("notes").value;
var calSuccess = function(message) { alert("Success: " + JSON.stringify(message)); };
var calError = function(message) { alert("Error: " + message); };


function calendarDemoAdd() {
  window.plugins.calendar.createEvent(title,where,notes,startDate,endDate,calSuccess,calError);
}

function calendarDemoRemove() {
  window.plugins.calendar.deleteEvent(title,where,notes,startDate,endDate,calSuccess,calError);
}

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助:

首先尝试解析日期,例如answer

function parseDate(input) {
  var parts = input.split('-');
  // new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based
}