循环使用不同元素的div

时间:2016-04-30 19:34:10

标签: javascript jquery

我生成了元素并将它们放在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
        }

2 个答案:

答案 0 :(得分:0)

我想这就是你想要的。

&#13;
&#13;
$(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);
  })
  
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="MyDiv"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var arrText = [];

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

console.log( arrText );