python - jquery动态加载select下拉框,如何做到每次点击下拉框都保证是最新的数据?
天蓬老师
天蓬老师 2017-04-17 15:35:25
0
6
412

使用jquery ajax 动态生成下拉框代码如下:

$.ajax({
                type:"get",
                dataType:"json",
                contentType:"application/json;charset=utf-8",
                url:"{{ url_for('ajax_categorys') }}",
                success:function(result){
                    $.each(result,function(index,value){
                        $("#ca").append("<option value='"+value.name+"'>"+value.name+"</option>");
                    })
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
            },
                async:false             //false表示同步
                 }

                );

这样可以在页面生成的时候动态加载下拉框数据,但当下拉框数据有改变后,再次点击仍然是初始加载的数据。

使用click事件则可以加载新的数据,但是会在已有数据下添加,则会有重复的数据出现。

在ajax里加上$("#ca").empty(); 可以清空现有选项,但是没法选择我需要的选项,因为当我点击其中一项选项时,又触发了另一次click事件。。。

$("#ca").click(function(){
                $("#ca").empty();
                 $.ajax({
                type:"get",
                dataType:"json",
                contentType:"application/json;charset=utf-8",
                url:"{{ url_for('ajax_categorys') }}",
                success:function(result){

                    $.each(result,function(index,value){

                        $("#ca").append("<option value='"+value.name+"'>"+value.name+"</option>");
                    })
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
            },
                async:false             //false表示同步
                 }

                );
            });

我如何才能使每次得到的下拉框选项都是最新的数据并且可以选中为需要的选项??

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(6)
左手右手慢动作
    When
  1. clicks #ca, check whether there is options_loaded=1, and if there is, no ajax request will be initiated. If not:
  2. Initiate an ajax request to obtain the latest data.
  3. Clear all options before appending options.
  4. After
  5. is appended, add the attribute #ca to the options_loaded=1 element.

The premise is that after the default user opens this page, clicks #ca, and loads the data, the data will not be updated. If the needs are opposite, then another idea needs to be used.

伊谢尔伦

1. Use html() instead of append() so you don’t need to clear it every time you click
2. If the click event prevents the event from bubbling, the update event will not be triggered by clicking on the option

小葫芦

1. If you want new data every time, you need to clear the cache and add a timestamp after the url;
2. Before loading data each time, clear the previously added data. You can also use the html()
mentioned above. var strHtml="";
$.each(result,function(index,value){
if(selected condition){

       strHtml+="<option selected="selected" value='"+value.name+"'>"+value.name+"</option>";
   }else{
       strHtml+="<option value='"+value.name+"'>"+value.name+"</option>";
   }

})
$("#ca").html(strHtml);

Ty80

When making an ajax request, first record the currently selected id to a global variable, such as (origin_id)
Then after requesting the data, if the value of each id and origin_id is the same, add selected=true
Then use innerHtml to replace the dom tree

PHPzhong

I don’t know what your list is used for. It requires such strong synchronization of data. Usually drop-down lists read data from the cache. Even business data is loaded once on the page and read from the background.

PHPzhong

The general approach is to directly write the json file for fixed options, which will load quickly. If it is non-fixed, use Ajax.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!