我正在構建一個嚮導作爲學習項目,我希望對整個嚮導中最後一頁中檢查的所有單選按鈕進行總結......我是否通過數組來完成此操作?如果是這樣,我如何將每個單選按鈕值添加到數組,然後將其記錄到摘要頁面?還有另一種方法可以做到這一點嗎?請幫忙。添加到數組中的單選按鈕值?
回答
有多種方式,我會建議單頁網頁架構,並在應用程序級別保存所有表單數據(單選按鈕/或任何其他數據),以便您可以在摘要頁面中訪問它。
下面是一個例子,希望這或多或少給你所需要的:
HTML
//First HTML DIV
<div>
<input type="radio" name="image_size" value="100x100"> 100x100<br>
<input type="radio" name="image_size" value="200x200"> 200x200<br>
<input type="radio" name="image_size" value="500x500"> 500x500<br>
<input type="button" value="Next" id="Next_ImageSize" />
</div>
//Second HTML DIV
<div>
<input type="radio" name="image_color" value="blue"> blue<br>
<input type="radio" name="image_color" value="green"> green<br>
<input type="radio" name="image_color" value="yellow"> yellow<br>
<input type="button" value="Next" id="Next_ImageColor" />
</div>
//Summary HTML DIV
<div>
<h1>Summary</h1>
<p id="values"><p>
</div>
JAVASCRIPT:(使用jQuery庫)
var _SummaryArr = []; //empty array
var _SummaryObj = []; //empty array (used for array of objects)
//Add the selected radio button to the array
$("[name='image_size']").change(function() {
_SummaryArr[0] = $(this).val(); //Index 0 for the first div
console.log(_SummaryArr[0]); //log to see the result
});
//Add the selected radio button to the array
$("[name='image_color']").change(function() {
_SummaryArr[1] = $(this).val(); //Index 1 for the second div
console.log(_SummaryArr[1]); //log to see the result
});
//Add the selected radio button to the array
$("[name='image_size']").change(function() {
_SummaryObj[0] = { "image_size" : $(this).val() }; //Index 0 for the first div
console.log(_SummaryObj[0]); //log to see the result
});
//Adding the values to the summary using array
for (var i = 0; i < _SummaryArr.length; i++) {
if (i == 0)
$("#values").html("<span>Image size: " + _SummaryArr[i] + "</span>");
if (i == 1)
$("#values").html("<span>Image color: " + _SummaryArr[i] + "</span>");
}
//Adding the values to the summary using object array
for (var i = 0; i < _SummaryObj.length; i++) {
if (i == 0)
$("#values").html("<span>Image size: " + _SummaryObj[i].image_size + "</span>");
}
JAVASCRIPT:(Native)
//Get all the radio button elements by name
var rad = document.getElementsByName('image_size');
//Iterate through all the buttons and assign click event listener
for(var i = 0; i < rad.length; i++) {
rad[i].onclick = function() {
_SummaryObj[0] = {"image_size":this.value}; //for the first div
//or
//_SummaryObj.push({"image_size":this.value});
};
}
for (var i = 0; i < _SummaryObj.length; i++) {
if (i == 0) //simple check to assign the name (this can easily be replaced by some dynamic function)
document.getElementById("values").innerHTML = "Image size:" + _SummaryObj[i].image_size;
}
謝謝你的幫助!我不使用jQuery,有沒有辦法只用JS來完成它? – tttt
現在將使用原生javascript解決方案進行更新。 – ThatAwesomeCoder
你有好友。 – ThatAwesomeCoder
- 1. Angular 2 - 將選中的單選按鈕的值添加到數組
- 2. 使用javascript在單獨的組中添加單選按鈕值
- 3. 如何在按鈕組中添加單選按鈕?
- 4. 動態添加單選按鈕值
- 5. 將所選單選按鈕的值添加到公式
- 6. 傳遞單選按鈕的數組值
- 7. 無法檢索單選按鈕組中的選中值按鈕
- 8. ASP淨中繼單選按鈕組的名稱,並得到單選按鈕值
- 9. 添加單選按鈕
- 10. 將單選按鈕添加到Tkinter
- 11. Zend_Form:添加元素到單選按鈕
- 12. 添加單選按鈕到hook_form
- 13. 將單選按鈕添加到R數據表中的閃亮
- 14. 在運行時向JQuery按鈕組添加單選按鈕
- 15. 單擊提交按鈕後要添加到數組中的變量的值
- 16. 將CSS添加到選中的單選按鈕並在jQuery中添加標籤
- 17. C#幫助爲菜單添加單選按鈕/選項按鈕
- 18. 如何添加單選按鈕到Tkinter的子菜單中
- 19. 將onclick事件添加到動態添加的單選按鈕
- 20. 將JSON數據添加到單選按鈕中
- 21. 添加一個單選按鈕組到定製對話框
- 22. 將jQuery驗證規則添加到單選按鈕組
- 23. 添加一個JRadioButton到單選按鈕組
- 24. 添加一個單選按鈕到2組
- 25. php數組到單選按鈕
- 26. 將類添加到HTML選擇,取決於Angular JS中的單選按鈕值。
- 27. 如何將值添加到單選按鈕並計算答案?
- 28. Javascript將值添加到單選按鈕以輸入價格
- 29. 添加多個單選按鈕組中動態的Android
- 30. 使用按鈕單擊將值添加到組合框
當您參考頁面,這意味着將有下一個選項轉到下一頁?你使用/將使用什麼樣的持久性? – ThatAwesomeCoder
我有4個div有上一個/下一個按鈕(圖像大小,圖像背景,圖像填充,圖像顏色)。用戶將選擇一個選項,例如他們可以選擇從三個選項中更改圖像大小:100x100/200x200/500x500,然後在圖像大小下的摘要頁面上,它將具有他們選擇的單選按鈕值,例如圖像大小:100x100 – tttt
那麼是的,在這種情況下,一個JavaScript數組可以工作得很好,如果你必須去一個完全不同的頁面,那麼像必須使用會話對象或者將數據保存到數據庫一樣,必須進行某種持久化。 – ThatAwesomeCoder