получить данные из ввода в массив - PullRequest
0 голосов
/ 28 февраля 2012
<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
    <p>
        <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /> <input type="checkbox" id="c_scnt" name="c_scnt" class="show"> <input type="text" id="more" name="more" class="hide"> </label>
    </p>
</div>

<span id="getall">Get all</span>

ВСЕ КОД: http://jsfiddle.net/tZPg4/1420/

Можно нажать Получить все и получить все данные со всех входов, а затем использовать их с каждым циклом и получить данные: this.one (первый ввод (текст)), this.two (второй вход (флажок)), this.three (третий вход - скрытый)? например:

$("#getall").click(function(){

        //here get all data to array, but how?

        array.each(function(i){
           var html = $("<tr><td>this.one</td><td>this.two</td><td>this.three</td></tr>").appendTo("#myTable");
        });

    })

LIVE: http://jsfiddle.net/tZPg4/1420/

Ответы [ 2 ]

1 голос
/ 28 февраля 2012

Как-то так должно работать?

$("#getall").click(function(){
      var myRow = document.createElement("tr");

      var array = new Array()
      $.each($("#p_scents").find("p"), function(ind, elem) {
          var inputCol = new Array();
          console.log("Adding row");
          $.each($(elem).find("input"), function(ind2, inputElem) {
              if ($(inputElem).attr("type") != "checkbox") {
                 inputCol [inputCol .length] = $(inputElem).val();                  
              }
              else {
                  inputCol [inputCol .length] = $(inputElem).is(":checked");
              }
          });
          array[array.length] = inputCol;
      });
        console.log("Outputting results");
        for (var i in array) {
            console.log("Row: "+i);
            for (var j in array[i]) {
               console.log("Input: "+j + " == " + array[i][j]);         
            }                    
        }

    });
0 голосов
/ 28 февраля 2012
var arr = new Array();

$( 'input' ).each(function(index)
{
    arr[arr.length] = $(this).val();
})
...