简体   繁体   English

ajax / jquery尝试附加新选项以进行选择

[英]ajax / jquery trying to append new options to select

I'm trying to load a list of locations via ajax / jquery. 我正在尝试通过ajax / jquery加载位置列表。 Each time I type a letter into the input I get the first respponse back with subsequent responses appended. 每次我在输入中输入字母时,都会得到第一个响应,并附加后面的响应。 I've tried using .html('') and .empty() but not getting the desired results. 我尝试使用.html('')和.empty(),但未获得所需的结果。 I want the list to clear each time and return the new input results. 我希望每次清除列表并返回新的输入结果。

的HTML

阿贾克斯

屏幕截图

See this my example , clear select first by empty and then append new option value !!! 看到我的示例,先清除选择为empty ,然后附加新的option值!

 $('#ss').empty(); $('#ss').append($('<option>').html('test')); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="ss"> <option>$$</option> <option>55</option> </select> 

Select all option and .remove() should work 选择所有option.remove()应该可以工作

 function clearSelectFields(){ $('#selectField') .children() // Select children of the select field .remove(); // Remove all the selected element } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="selectField"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <button onclick="clearSelectFields()">Clear</button> 

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

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