javascript - 如何在 ajax 响应后填充 radio div?

标签 javascript php jquery ajax

我想在其中附加 html 的 Html Div:

<div class="col-md-12" style="border:1px solid green;height: 20px;"id="eng_skills"></div> 

jQuery 和 AJAX 代码在这里。响应包含我想附加到上层 html 中的 HTML:

$(document).ready(function(){
   var jobid=$("#eng_jobtitle").val();
   var worker_id=$("#worker_id").val();
   //alert(jobid);
   //alert(worker_id);
    $.ajax({

          type: 'POST',
          url: '/admin/listing-details/get_skills',
          data: {
                "_token": "{{ csrf_token() }}",
                "job_title_id" :jobid,
                "worker_id" : worker_id
          },
          success: function (response)
           {

             html+='<div class="form-group">';
             html+='<span class="col-md-6 col-xs-12  no-padding set_text">';
             $.each(response,function(i,val){
              html+='<label class="">'+val.skill_eng+'</label></span>';
              html+='<input type="radio"  name="'+ i +'" id="'+ i +'"/>';


             })
             html+='<span class="col-md-6 col-xs-12 set_text">';
             html+='<input type="radio" name="cooking"><label class="">Yes</label>';
             html+='<input type="radio" name="cooking"><label class="">No</label>';
             html+='</span>';
             html+='</div>';
            $('#eng_skills').html(html);

           }
  })
})

我得到的 Ajax 是:

{"status":true,
"result":
    [
{"id":1,"job_id":1,"skill_eng":"cleaning","skill_arb":"\u062a\u0646\u0638\u064a\u0641","ordering":1,"added_date":"2018-01-31 00:00:00"},   
     {"id":2,"job_id":1,"skill_eng":"cooking","skill_arb":"\u062a\u0646\u0638\u064a\u0641","ordering":2,"added_date":"2018-01-31 00:00:00"},      
     {"id":3,"job_id":1,"skill_eng":"utensils washing","skill_arb":"\u0623\u0648\u0627\u0646\u064a \u0627\u0644\u063a\u0633\u064a\u0644","ordering":3,"added_date":"2018-01-31 00:00:00"},
     {"id":4,"job_id":1,"skill_eng":"cloths washing","skill_arb":"\u063a\u0633\u0644 \u0627\u0644\u0645\u0644\u0627\u0628\u0633","ordering":4,"added_date":"2018-01-31 00:00:00"}
]
}

创建响应后,我无法按需要生成输入类型的 radio 。它没有附加在 html 上,为什么会这样?

最佳答案

你循环的数组是错误的

对响应['result']进行操作

作为

$.each(response['result'],function(i,val)){
     //code inside
 })

同时定义 var html = '';在开始

关于javascript - 如何在 ajax 响应后填充 radio div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48537395/

相关文章:

php - 如何从sql中以逗号分隔的表中检索数据?

jQuery 处理程序错误不是一个函数

jquery - ASP.NET MVC 4 : client validation not work

javascript - 使用 jQuery 或 Underscore.js 获取嵌套对象中字段的值

Asp.net 将文本框 1 设置为等于文本框 2

javascript - jQuery 在 JSFiddle 中有效,但在本地 RoR 中无效。类型错误?

javascript - 如何从 Blob 创建文件对象?

php - PHP减去字符串的第一个字符

php - 使用 mysql 查询每隔几秒刷新一次文本框?

php - 使用 php mysql 和 jquery 显示数据库动态内容