使用不同的元素循环div



我生成了元素并将它们放在MyDiv 中

 $("#MyDiv").append('<input type=text class = "form-control" id=tb' + row.PropertyName + ' ' + 'value="Text' + row.PropertyName + '" />');
 $("#MyDiv").append('  <input type="hidden" name="hid" value= "' + row.PropertyId + '">');

现在我需要提取row.PropertyNamerow.PropertyId

我需要这样的东西:

    var arrText = new Array();
    $('#MyDiv > input[type = text]').each(function () {
        var id = $(this).id.val();
        var text = $(this).text.val();
        var data = {
            'id': id,
            'text': text
        }

我想这就是您想要的。

$(function(){
  var PropertyName = "pn1";
  var PropertyId = "pn2";
$("#MyDiv").append('<input type="text" class = "form-control" id="tb"' + PropertyName + ' ' +
                       'value="Text' + PropertyName + '" />');
 $("#MyDiv").append('  <input type="hidden" name="hid" value= "' + PropertyId + '">');
  
  $('#MyDiv > input[type = "text"]').each(function(){
    
    var id = $(this).val();
    var text = $(this).next('input[type = "hidden"]').val();
    var data = {
      id: id,
      text: text
    }
    alert(data.id + data.text);
  })
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="MyDiv"></div>

var arrText = [];
$('#MyDiv > input[type=text]').each(function () {
    arrText.push({
        'id': this.id,
        'text': this.value
    });
});
console.log( arrText );

最新更新