Массив из .val
Всем привет и хорошего настроения)) нужна помощь в общем есть парочку инпутов с классом "i_am_input"
<input type='text' class='i_am_input' value='good'> <input type='text' class='i_am_input' value='bad'> <input type='text' class='i_am_input' value='good'> <input type='text' class='i_am_input' value='bad'> <input type='text' class='i_am_input' value='good'> и переменная var i_am_peremen=0; и мне нужно пробежаться массивом по всем импутам и если value == 'good' то мы к переменной плюсуем один заранее огромное спасибо за помощь) |
Trues,
:cray:
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<script>
function init() {
function fn(sum,cur) {
return cur.value == "good" ? ++sum : sum
}
var i_am_peremen = Array.prototype.reduce.call(document.querySelectorAll(".i_am_input"), fn, 0)
alert(i_am_peremen)
}
window.onload = init;
</script>
</head>
<body>
<input type='text' class='i_am_input' value='good'>
<input type='text' class='i_am_input' value='bad'>
<input type='text' class='i_am_input' value='good'>
<input type='text' class='i_am_input' value='bad'>
<input type='text' class='i_am_input' value='good'>
<script>
</script>
</body>
</html>
|
<html>
<head>
</head>
<body>
<input type='text' class='i_am_input' value='good'>
<input type='text' class='i_am_input' value='bad'>
<input type='text' class='i_am_input' value='good'>
<input type='text' class='i_am_input' value='bad'>
<input type='text' class='i_am_input' value='good'>
<script>
i_am=0
;[].forEach.call(document.querySelectorAll(".i_am_input"), function(el){
if(el.value==="good") i_am++
})
alert(i_am)
</script>
</body>
</html>
|
Рони почему такой смайл)))
|
Trues,
потому что надо читать документацию https://learn.javascript.ru/searching-elements-dom https://learn.javascript.ru/array-iteration |
Понял тебя Рони)) всем большое спасибо)) помогли с задачей справился:)
|
<input type='text' class='i_am_input' value='good'>
<input type='text' class='i_am_input' value='bad'>
<input type='text' class='i_am_input' value='good'>
<input type='text' class='i_am_input' value='bad'>
<input type='text' class='i_am_input' value='good'>
<script>window.onload = function(){
alert(document.querySelectorAll('.i_am_input[value="good"]').length);
};</script>
|
Poznakomlus,
:) |
| Часовой пояс GMT +3, время: 04:34. |