javascript 以html形式添加ajax成功返回json对象数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28980859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:52:55  来源:igfitidea点击:

Add ajax success return json object data in html form

javascriptphpjqueryajaxjson

提问by rjcode

I have created form to add data in php-mysql database with ajax, It is working fine, after data is added it shows in html table.

我已经创建了使用ajax在php-mysql数据库中添加数据的表单,它工作正常,添加数据后它显示在html表中。

Now i am trying to create code for editing those entry, I had created code as below for edit with AJAX,

现在我正在尝试创建用于编辑这些条目的代码,我创建了如下用于使用 AJAX 进行编辑的代码,

$(document).ready(function() {
var table = $('#categorylist').DataTable();

$('#categorylist tbody').on( 'click', 'tr', function () {
    $(this).toggleClass('selected');
} );

$('#edit_category').on( 'click',function () {
    document.getElementById('editcategory').style.display = "block";



    /*do some task here to get values of ID*/
var catid = $("#cat_id").val();


            jQuery.ajax({
                        url: "<?php echo base_url(); ?>index.php/tools/edit_category",
                        data: {category:catid},
                        type: "POST",
                        success:function(data){ 
                        var objData = jQuery.parseJSON(data);
                            console.log(objData);
                            $("#cname").html(
                                objData.category_name
                                            );


                                    }
                        });

});
});

Above code will send request to PHP to get information for that ID and return me data in json format, Below is returned data,

上面的代码会向 PHP 发送请求以获取该 ID 的信息并以 json 格式返回我的数据,下面是返回的数据,

category_id: "38" category_name: "Test Conveyance"

category_id: "38" category_name: "测试运输"

Now i want to show this values in html form so that user can see what are current values, then user will edit values and click on save.

现在我想以 html 形式显示这些值,以便用户可以看到当前值是什么,然后用户将编辑值并单击保存。

Below is my html form, how can i show category ID and name in html input form as value?

下面是我的 html 表单,如何在 html 输入表单中显示类别 ID 和名称作为值?

Thanks,

谢谢,

<form action="" method="post" accept-charset="utf-8" id="category_editform">
                            <div class="form-group">

                              <div class="controls">
                              <div class="input-group m-t-20 icons-panel"> <span class="input-group-addon bg-white"> <i class="fa fa-list"></i> </span>
                         <input type="text" class="form-control" name="category_name" placeholder="category name" id="cname" required>
                        </div>

                              </div>
                            </div>
                            <div class="align-left">
                              <input type="submit" class="btn btn-primary m-r-20" name="create_category" value="Save">
                              <button type="reset" class="btn btn-default" onClick="expensecategory_editform_hide()">Cancel</button>
                            </div>

                          </form>

回答by ajaykumartak

imagine this is your json

想象这是你的json

{'category_id': "38", 'category_name': "Test Conveyance"}

parse it using dot notation. Ex. if it in ajax response use data.category_idand data.category_name

使用点表示法解析它。前任。如果它在 ajax 响应中使用data.category_iddata.category_name

Then

然后

use set it into form using id Ex. your field is

use 使用 id Ex 将其设置为表单。你的领域是

<input type="text" id="category_id">

then assign the value of category id to field

然后将类别 id 的值分配给字段

$("#category_id").val(data.category_id);

And in your case you should use

在你的情况下,你应该使用

$("#cname").val(objData.category_name);

回答by Sameer K

Use below code to show category name value in html form

使用以下代码以 html 形式显示类别名称值

$("#cname").val(objData.category_name)

$("#cname").val(objData.category_name)

Instead of

代替

$("#cname").html(objData.category_name)

$("#cname").html(objData.category_name)