Javascript JQuery将数据附加到html选择标记

Javascript JQuery将数据附加到html选择标记,javascript,jquery,html,Javascript,Jquery,Html,我有以下Jquery脚本,它以json的形式从数据库检索数据: $(document).ready(function() { $.ajax({ type: "GET", url: "database/Emarps/websrvc/websrvc.php?task=getData&UUID=" + currentuuid + "&DataGt=hk", dataType: "JSON", success: function(response) {

我有以下Jquery脚本,它以json的形式从数据库检索数据:

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "database/Emarps/websrvc/websrvc.php?task=getData&UUID=" + currentuuid + "&DataGt=hk",
    dataType: "JSON",
    success: function(response) {
      $('#PrevOptns').val(response[0].hiv_prev);
      $('#HivTrans').val(response[0].hiv_trans);
    }
  });

});
如何传递数据以反映或附加到以下HTML select标记select标记接受多个选择,返回的数据可以是多个select:

<select required name="HivTrans" id="HivTrans" size="7" multiple class="select_with_label_wide">

  <option id="Handshake and close body contact">Handshake and close body contact</option>
  <option id="Mosquito's and other insects">Mosquito's and other insects</option>

  <option id="Kissing">Kissing</option>
  <option id="Sharing of needles/Syringes">Sharing of needles/Syringes</option>
  <option id="Other">Other</option>
</select>

<select required name="PrevOptns" id="PrevOptns" size="7" multiple class="select_with_label_wide">

  <option id="Avoid mosquito bites">Avoid mosquito bites</option>
  <option id="Get protection from traditional healer">Get protection from traditional healer</option>
  <option id="Other">Other</option>
  <option id="Don't know">Don't know</option>
</select>

如果要附加所有数据,请尝试执行“for each”循环

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "database/Emarps/websrvc/websrvc.php?task=getData&UUID=" + currentuuid + "&DataGt=hk",
    dataType: "JSON",
    success: function(response) {
      for (var i=0; i<response.length; i++) {
         $('#PrevOptns').append('<option value='+response[i].hiv_prev+'>'+response[i].hiv_prev+'</option>');;
         $('#HivTrans').append('<option value='+response[i].hiv_trans+'>'+response[i].hiv_trans+'</option>');
      }
    }
   });

 });
jQuery有一个方法,可以用来将内容插入html元素的末尾。构建您的元素,并简单地将它们附加到相关的select标记

JS


你能在这里发布json响应吗
$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "database/Emarps/websrvc/websrvc.php?task=getData&UUID=" + currentuuid + "&DataGt=hk",
    dataType: "JSON",
    success: function(response) {
      for (var i=0; i<response.length; i++) {
         $('#PrevOptns').append('<option value='+response[i].hiv_prev+'>'+response[i].hiv_prev+'</option>');;
         $('#HivTrans').append('<option value='+response[i].hiv_trans+'>'+response[i].hiv_trans+'</option>');
      }
    }
   });

 });
success: function (response) {
    var prevOpts =  "<option value='" +response[0].hiv_prev +"'</option>";
    var hivTrans=  "<option value='" +response[0].hiv_prev +"'</option>";

    $("#PrevOptns").append(prevOpts);
    $("#HivTrans").append(hivTrans);

}