如何在动态输入中从jquery获取值

时间:2018-01-21 20:33:26

标签: javascript jquery codeigniter

我试图用答案或者创建问题 在 CodeIgniter 中进行多项选择,我使用jQuery创建了选项,现在我不知道如何从文本输入中获取所有值。

有人可以帮我解决这个案子吗?

此代码:



var choices = [{
  id_soal: 'choice1'
}, {
  id_soal: 'choice2'
}, {
  id_soal: 'choice3'
}];
var html = '';
var i;
for (i = 0; i < choices.length; i++) {
  html += '<div class="row">',
    html += '<div class="col-xs-8 col-md-4>',
    html += '<div class="input-group">',
    html += '<span class="input-group-addon" style="background:green"><i class="fa fa-question-circle"></i>  Soal' + (i + 1) + '</span>',
    html += '<input type="text" name="Question' + i + '" id="Question' + i + '" class="Question form-control" placeholder="Question" required>',
    html += '</div></div></div></br>',

    html += '<div class="row">',
    html += '<div class="col-xs-4 col-md-4">',
    html += '<div class="input-group">',
    html += '<span class="input-group-addon">A</span>',
    html += '<input type="text" name="A_jawaban' + i + '" id="A_jawaban' + i + '" class="form-control A_jawaban" placeholder="Result" required>',
    html += '</div></div>'
  html += '<div class="col-xs-4 col-md-4">',
    html += '<div class="input-group">',
    html += '<span class="input-group-addon">  B</span>',
    html += '<input type="text" name="B_jawaban' + i + '" id="B_jawaban' + i + '" class="form-control" placeholder="Result" required>',
    html += '</div></div>',
    html += '<div class="col-xs-4 col-md-4">',
    html += '<div class="input-group">',
    html += '<span class="input-group-addon">  C</span>',
    html += '<input type="text" name="C_jawaban' + i + '" id="C_jawaban' + i + '" class="form-control" placeholder="Result" required>',
    html += '</div></div></div><br>';

  html += '<div class="row">',
    html += '<div class="col-xs-4 col-md-6">',
    html += '<div class="input-group">',
    html += '<span class="input-group-addon">  D</span>',
    html += '<input type="text" name="D_jawaban' + i + '" id="D_jawaban' + i + '" class="form-control" placeholder="Result" required>',
    html += '</div></div>'
  html += '<div class="col-xs-4 col-md-6">',
    html += '<div class="input-group">',
    html += '<span class="input-group-addon">  E</span>',
    html += '<input type="text" name="E_jawaban' + i + '" id="E_jawaban' + i + '" class="form-control" placeholder="Result" required>',
    html += '</div></div></div><br>';
}
$('.judul').html(html);

$('#tambah').click(function(event) {

  console.log('THIS CHOICES',choices)
  var results = $('.Question').serializeArray();
  console.log('FOR QUESTIONS',results)
var resultsAnswearA = $('.A_jawaban').serializeArray();
  console.log('FOR QUESTIONS',resultsAnswearA)




})
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<div name="judul" class="judul"></div>

  <button id="tambah" name="tambah" class="btn btn-warning"><i class="icon-pencil5"></i> Tambah</button>
      
&#13;
&#13;
&#13;

更新

对不起我上面的问题,我忘记了,只是意识到我得到了使用查询选择器的答案。只需检查代码

1 个答案:

答案 0 :(得分:0)

尝试:

var allInputsValue = {};

$(".judul input").each(function(){
    //Add each input value to all inputs
    allInputsValue[allInputsValue.length] = {
        "name":$(this).attr("name"),
        "value":$(this).val()
    };
});

console.log(allInputsValue);