2014-01-25 47 views
0

嗨我有一個表單,但是當它提交時,所有的字段都是空白的。我在這裏做錯了什麼?var document.getElementById(「title」)。value;問題

<form><p> 
     Remind me about: <input type="text" id="title"><br> 
     Where: <input type="text" id="where"> 
     Notes: <input type="text" id="notes"><br> 
     Start Date and Time: <input type="datetime" id="startDate" placeholder="October 13, 1975 11:10:00"> 
     End Date and Time: <input type="datetime" id="endDate" placeholder="October 13, 1975 11:15:00"><br> 
     <input type="submit" onclick="calendarDemoAdd(this.form)"></p> 
     </form> 

和JavaScript:

var startDate = new Date(document.getElementById("startDate").value); 
var endDate = new Date(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); 
} 

回答

1

你沒有給輸入元素name屬性。

只有帶名稱的控件才能成功。

這是可能的(雖然它會失敗遵循Progressive EnhancementUnobtrusive JavaScript原則)來代替數據收集和<form>使用JavaScript的編碼功能,但沒有跡象表明你對JS是這樣做。

+0

嗨昆汀。我按照建議添加了名字,但仍然只有空白字段。還有別的嗎? 如果我做了以下工作,它工作正常: var title =「這是一個標題」; etc etc – user2110655

+0

如果問題是將數據讀入JavaScript(與提交表單相反,如問題所述),那麼您需要提供更多有關JS正在做什麼的信息,以及觸發它,你如何測試這些值,以及瀏覽器的JavaScript錯誤控制檯正在報告什麼。 – Quentin

1

貌似name屬性丟失所有輸入字段

<form><p> 
    Remind me about: <input type="text" name="title" id="title"><br> 
    Where: <input type="text" name="where" id="where"> 
    Notes: <input type="text" name="notes" id="notes"><br> 
    Start Date and Time: <input type="datetime" name="startDate" id="startDate" placeholder="October 13, 1975 11:10:00"> 
    End Date and Time: <input type="datetime" name="endDate" id="endDate" placeholder="October 13, 1975 11:15:00"><br> 
    <input type="submit" onclick="calendarDemoAdd(this.form)"></p> 
    </form> 
0

實際修復是

function calendarDemoAdd() { 
var title = document.getElementById("title").value; 
var where = document.getElementById("where").value; 
var notes = document.getElementById("notes").value; 
    window.plugins.calendar.createEvent(title,where,notes,startDate,endDate,calSuccess,calError); 
} 

的原因是,對變量進行填充已運行的功能之前。

相關問題