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

时间:2015-09-25 11:06:18

标签: 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) {
      $('#PrevOptns').val(response[0].hiv_prev);
      $('#HivTrans').val(response[0].hiv_trans);
    }
  });

});

如何传递数据以反映或附加在以下HTML选择标记上(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>

3 个答案:

答案 0 :(得分:0)

 $("#PrevOptns").append('<option value='+response[0].hiv_prev+'>'+response[0].hiv_prev+'</option>');
    $("#HivTrans").append('<option value='+response[0].hiv_trans+'>'+response[0].hiv_trans+'</option>');
   $('#PrevOptns').trigger("chosen:updated");
   $('#HivTrans').trigger("chosen:updated");

答案 1 :(得分:0)

如果你想附加所有数据,请尝试'为每个'循环。

<script type="text/javascript">
    //alert("Welcome to our visitors from  "+geoplugin_currencyCode());  
    alert("Welcome to our visitors from  "+geoplugin_countryName());

     $flag=false;
     if($flag===false) {
        var country = geoplugin_countryName();

        if(country === 'India') {
            $('#currency input[name=\'code\']').attr('value', 'INR');
            $('#currency').submit();
            alert('Your currency is set to INR');
        } else {
            $('#currency input[name=\'code\']').attr('value', 'USD');
            $("#currency").change();
            alert('Your currency is set to USD');
        }
   } else {
        return false;
   }
</script>

答案 2 :(得分:0)

jQuery有一个append方法,您可以使用该方法将内容插入到html元素的末尾。构建元素并简单地将它们附加到相关的选择标记。

<强> JS

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);

}