Javascript:通过显示模式的ajax更新输入值

时间:2016-03-30 18:47:01

标签: javascript jquery ajax twitter-bootstrap laravel-5.1

我有一个模态,我在元素点击时显示它。 所以我想通过Ajax更新这个模态中的输入值,我可以成功检索数据但是它没有正常更新它的值(获取其他文章记录的值而不是当前的articleID),这里是我的代码:

$('#editArticle').on('show.bs.modal', function (e) {
    ...
    $articleID =  $(e.relatedTarget).attr('data-id');
    $.ajax({
            type: "GET",                
            url : 'article/'+ $articleID +'/getData',                
            dataType : "json",
            success : function(data){                    
                document.getElementById("serial").value = data['serial'];
            }
    });  
});

如果我设置警告显示数据,则返回正确的值:

success : function(data){ 
                alert(data['serial']); // here it returns the right value for each row, and it appears after the modal show !!!!                   
                document.getElementById("serial").value = data['serial'];
            }

记录列在数据表中: enter image description here  这是什么错误?!

1 个答案:

答案 0 :(得分:0)

IMO,首先尝试克隆模态,然后在克隆模态中找到id或类。 在这个例子中,我使用引导对话框作为插件。 请看一下:

var modal = $('.modal').clone();

BootstrapDialog.show({
    title: 'blabla',
    message: modal.show(),
    onshow: function() {
      $(modal).find('#result').text('updated text');
    },
    buttons: [{
      label: 'Cancel',
      action: function(dialogRef) {
        dialogRef.close();
      }
    }]
});
<div class="modal" style="display:none">
  <div id="result"></div>
</div>

希望此代码适用于您的问题