2013-01-03 46 views
0

我有以下标记收集值创建动态

<span id="add"> 

    <li style="float: left;margin-right: 10px;"> 
    <b class="bg">Nom</b><br /> 
    <input type="text" class="_input _input_text nom"/> 
    </li> 

    <li> 
    <b class="bg">montant investi</b><br /> 
    <input type="text" class="_input _input_text montant"/> 
    <a class="_button" 
     onclick="$('#add').append(
      '<span>\ 
      <li style=\'float: left;margin-right: 10px;\'>\ 
       <b class=\'bg\'>Nom</b><br />\ 
       <input type=\'text\' class=\'_input _input_text nom\'/>\ 
      </li>\ 
      <li>\ 
       <b class=\'bg\'>montant investi</b><br />\ 
       <input type=\'text\' class=\'_input _input_text montant\'/>\ 
       <a class=\'_button\' style=\'float:right;margin-top:0;margin-left:5px\' \ 
       onclick=\'$(this).parent().parent().remove();\'>\ 
       <b>X</b>\ 
       </a>\ 
      </li>\ 
      </span>');" 
     style="float: right;margin-top: 0px;margin-left: 5px;"> 
     Ajouter un associé 
    </a> 
    </li> 

</span> 

的onclick函数创建具有两个<li>的标记与内输入文本。有两类输入:.nom,.montant。

当我尝试使用这个功能来收集他们:

var _nom=[]; 

_nom.push($("#add input.nom[type=text]").val()); 

$.each(_nom, function(key, value) { 
    alert(key + ': ' + value); 
}); 

我只能得到不参与自动创建的两个输入值。

回答

1

您需要改用此:你数组元素的

$("#add input.nom[type=text]") 
.each(function(key, value) { 
    alert(key + ': ' + $(value).val()); 
}); 

http://jsfiddle.net/4UhqL/

它为您提供了错误的结果,因为$("#add input.nom[type=text]")的回报,而不是一个元素。

+0

谢谢!,完美的作品。 – user7832