2015-04-03 49 views
0

這是我的複選框元素:如何獲取多個複選框的最後一個值?

<input type="checkbox" name="colors" value="blue" /> Blue <br/> 
<input type="checkbox" name="colors" value="green" /> Green<br/> 
<input type="checkbox" name="colors" value="yellow" /> Yellow<br/> 

我怎樣才能從這種多選框獲得最後的價值,只是明確JS,因爲我一直在學習JS和HTML僅僅2天。謝謝。

回答

1

使用document.querySelectorAll

var checkboxes = document.querySelectorAll("input[name=colors]") 
var lastCheckbox = checkboxes[checkboxes.length - 1]; 
var lastCheckboxValue = lastCheckbox.value; 
相關問題