简体   繁体   English

在jQuery Mobile中动态选择组合框

[英]Select combo box dynamically in jquery mobile

I want to append value from data base on option tag combo box. 我想在选项标签组合框中附加来自数据库的值。 But when I first doing option tag's value empty and then I appending through AJAX then it only showing one value. 但是,当我首先将选项标签的值设为空,然后通过AJAX追加时,它仅显示一个值。 But I want like all option should be visible but one should selected automatically. 但是我希望所有选项都应该可见,但是应该自动选择一个。

my code 我的代码

$('#b').on("click", "a", function () {
    var patid= $(this).attr('id');
    $.ajax({
    type:"GET",
    url:"https://localhost/patient_details1.php?patid="+patid,
    dataType:'JSON',
    success:function(response)
    {
      ("#pat_type").html("");
       for (var i=0;i<response.length;i++) 
          {
            $('<option value="'+ response[i].pattype +'">'+ 
    response[i].pattype +'</option>').appendTo("#pat_type");
                    }
          }
    }
    });             
}); 
  1. use $('#pat_type').empty(). 使用$('#pat_type')。empty()。
  2. check when you get data from your request. 检查何时从请求中获取数据。 You need to get it only after loading your page. 仅在加载页面后才需要获取它。

You having typo error ("#pat_type").html(""); 您遇到拼写错误("#pat_type").html("");

make it $("#pat_type").html(""); 使它成为$("#pat_type").html(""); or $('#pat_type').empty(); $('#pat_type').empty();

for (var i = 0; i < response.length; i++){
       listItems+= "<option value='" + response[i].pattype + "'>" + response[i].pattype + "</option>";
    }

    $("#pat_type").append(listItems);

// for setting selected item based on value
$('#pat_type').val('selectedvalue');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM