使用jQuery动态更改下拉菜单的内容

时间:2020-10-28 02:40:09

标签: javascript php html jquery css

我对jQuery非常陌生。我想从给定的URL设置下拉菜单的内容。假设下拉列表为:

<!--filter1-->
  <h4>City Filter1</h4>
    <select id="filter1">
    <option>--Select--</option>
  </select>

  <!--filter2-->
  <h4>City Filter2</h4>
  <input type="Checkbox" class="filter2">Value1<br/>
  <input type="Checkbox" class="filter2">Value2<br/>
  <input type="Checkbox" class="filter2">Value3<br/>
  <input type="Checkbox" class="filter2">Value4<br/>

  <!--filter3-->
  <h4>City Filter3</h4>
  <input type="Checkbox" class="filter3">Value1<br/>
  <input type="Checkbox" class="filter3">Value2<br/>
  <input type="Checkbox" class="filter3">Value3<br/>
  <input type="Checkbox" class="filter3">Value4<br/>

<input type="Checkbox" class="filter2"><br/>


此后,我还想更改filter2和filter3复选框的内容。我不知道该怎么做,因为我是jquery的新手。

1 个答案:

答案 0 :(得分:3)

您可以将 ajax 与某些 jquery 结合使用

$.ajax({url: "http://Your_Sexy_Url.com", success: function(result){

    //Here you can process your response from url
    $("select").html(""); // remove old options from select
    var options = "";
    $.each(data, function(index) {
        var row = data[index];
        options += "<option>"+row+"</option>"
    });
    $("select").html(options); // add new options to select

}});

同样,您也可以更新input[type=checkbox]

相关问题