2015-08-27 85 views
0

我剛剛製作了一個表格。基本形式[Javascript/HTML]

我有一個問題訪問該窗體的值,並將它們存儲到變量中,稍後由函數使用。無論何時提交表單,它都會將您帶到另一個網頁。 JavaScript文件附加到表單和另一個網頁上。

我已經試過 - [如果我給ID到屬性]

var someVariable = document.getElementById("Some id").val();  
console.log(someVariable); // It shows a error in console, not the value. 

闖闖

var someVariable = document.forms["Form Name"]["Property name"].value(); 
console.log(someVariable); 

在控制檯的一些錯誤。

表單

<div id="ques"> 
<ul> 
<form name="myForm" action="Answer.html" method="get"> 
<li>What cuisine?(You can leave it empty)<br> 
<input list="Cuisines" class="normal" type="text" name="Cuisine" placeholder="Like Chinese" pattern="Chinese|North Indian|Italian|American"> 
<datalist id="Cuisines" autocomplete="off"> 
    <option value="Chinese"></option> 
    <option value="North Indian"></option> 
    <option value="Italian"></option> 
    <option value="American"></option> 
</datalist></li> 
<li>On a scale of 1 to 10, how much hungry are you?<br><input class="normal" type="number" name="hunger" min="1" max="10" required></li> 
<li><input class="special" type="checkbox" name="Personality" value="Vegetarian"checked> Vegetarian 
<input class="special" type="checkbox" name="Personality" value="Non-Vegetarian" > Non-Vegetarian 
</li> 
<li>On a scale of 1 to 10, how much healthy do you want the food to be?<br><input class="normal" type="number" name="Calories" min="1" max="10" required></li> 
<li>What will be the max cost of the food, per person?<br>(Quality of the food will be affected)<br><input class="normal" type="number" name="quality" placeholder=" Like 400" step="50" required></li> 
<li>How many people?<br><input class="normal" type="number" name="Amount of people" required></li> 
<li><a href="Answer.html"><input class="normal" type="submit"></a></li> 
</form> 
</ul> 
</div> 

編輯

我的作品,與.value的,我怎麼能得到它登錄這兩個網頁。

+0

使用'.value'而不是'.value()' – bransonl

+0

謝謝!但仍未完成,它會記錄,但在表單網頁上。我在這兩個網頁上都使用了一個JavaScript文件。我應該製作另一個文件嗎? – Lavios

+0

如果它是一個完全獨立的頁面被重新加載,腳本將被重新加載並且變量重置。你需要找到一種方法來重新加載或通過查詢字符串傳遞你的變量。嘗試尋找jQuery來動態修改HTML,以便在重定向到下一頁時保留變量或查詢字符串以通過。 – bransonl

回答

2

.val()是一種用於輸入值的jQuery方法。

嘗試element.value


此外,xxx()始終是一個函數,但肯定是有一個單元內沒有可用的value()功能。