Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   Массив из .val (https://javascript.ru/forum/dom-window/55451-massiv-iz-val.html)

Trues 28.04.2015 16:37

Массив из .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' то мы к переменной плюсуем один

заранее огромное спасибо за помощь)

рони 28.04.2015 17:06

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>

nondeterminism 28.04.2015 17:13

<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 28.04.2015 17:13

Рони почему такой смайл)))

рони 28.04.2015 17:16

Trues,
потому что надо читать документацию
https://learn.javascript.ru/searching-elements-dom
https://learn.javascript.ru/array-iteration

Trues 28.04.2015 17:23

Понял тебя Рони)) всем большое спасибо)) помогли с задачей справился:)

Vlasenko Fedor 29.04.2015 00:24

<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>

рони 29.04.2015 00:54

Poznakomlus,
:)


Часовой пояс GMT +3, время: 10:58.