使用ajax

时间:2015-11-07 13:35:37

标签: jquery ajax twitter-bootstrap codeigniter

我需要帮助将数据传递给Codeigniter中带有ajax的模态引导程序

当我将数据传递给模态时,

模态引导程序有空白输入。我知道我的ajax工作,因为如果我传递静态数据,模态显示它。但是如果我从GET传递数据,模态输入总是空白

我是ajax的新手,所以我认为我的JSON解析是错误的。

型号:

public function getEditContent($table,$field,$key)
{
    return $this->db->get_where($table, array($field => $key))->result();
}

控制器:

public function Test()
{
    $this->load->library('Datatables');

    $this->_header();
    $this->load->view('Front/v_test',$data);
    $this->_footer();
}

public function testdata()
{
    $data['result'] = $this->Model->getEditContent('arsip','idsurat','1');
    echo json_encode($data);
}

查看:



<div class="col-xs-12 col-sm-12">
	<!-- Button HTML (to Trigger Modal) -->
	<a href="javascript:void(0)" onclick="openModal()">
		<span class="glyphicon glyphicon-pencil"></span></a>
</div>

<!-- Modal HTML -->
<div id="form-content" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<a class="close" data-dismiss="modal">×</a>
				<h3>Input Data Surat Keluar</h3>
			</div>
			<div class="modal-body">
				<?php echo form_open('Tambah/insert_suratkeluar'); ?>
				<div class="form-group">
					<label for="inputEmail">No Surat</label>
					<input type="text" class="form-control" name="nosurat" placeholder="No Surat" id="nosurat">
				</div>
				<div class="form-group">
					<label for="tglkirim">Tanggal Kirim</label>
					<div class='input-group date' id='datetimepicker1'>
						<input type='text' class="form-control" name="tglkirim" placeholder="Masukkan Tanggal Kirim" />
						<span class="input-group-addon">
							<span class="glyphicon glyphicon-calendar"></span>
						</span>
					</div>
				</div>
				<script type="text/javascript">
					$(function ()
						{
							$('#datetimepicker1').datetimepicker(
								{
									format: 'YYYY/MM/DD'
								});
						});
				</script>
				<div class="form-group">
					<label for="inputPassword">Alamat</label>
					<input type="text" class="form-control" name="alamat" placeholder="Alamat" id="alamat" value="">
				</div>
				<div class="form-group">
					<label for="notelp">No Telp</label>
					<input type="text" class="form-control" name="notelp" placeholder="No Telp" value="">
				</div>
				<div class="form-group">
					<label for="inputPassword">Email</label>
					<input type="email" class="form-control" name="email" placeholder="Email" value="">
				</div>
				<div class="form-group">
					<label for="inputPassword">Nama Perusahaan</label>
					<input type="text" class="form-control" name="nmusaha" placeholder="Nama Perusahaan" value="">
				</div>
				<div class="form-group">
					<label for="inputPassword">Deskripsi</label>
					<input type="text" class="form-control" name="deskripsi" placeholder="Deskripsi" value="">
				</div>
				<div class="modal-footer">
					<button type="submit" class="btn btn-primary">Submit</button>
					<a href="#" class="btn" data-dismiss="modal">Cancel</a>
				</div>
				<?php echo form_close(); ?>
			</div>
		</div>
	</div>
</div>
&#13;
&#13;
&#13;

Jquery:

function openModal()
{
    $.ajax(
        {
            url:"testdata/",
            type : 'GET',
            dataType : 'json',
            success:function (result)
            {
                $('#nosurat').val(result.nosurat);
                $('#alamat').val("Test Alamat");
                $('#form-content').modal('show');
            },

        });

}

JSON我进入了萤火虫:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果检查响应,则返回包含数组属性result的对象。因此,您可以通过传递适当的密钥来达到所需的价值:

$('#nosurat').val(result[0].nosurat);