When I was writing code today, I encountered a very strange problem:
$ ('# Department_parent'). combotree ('setvalue', row. ID );
AjaxForProvince(); $(‘#province‘).combobox(‘setValue‘, row.province_id); AjaxForCity(row.province_id); $(‘#city‘).combobox(‘setValue‘, row.city_id); AjaxForZone(row.city_id); $(‘#zone‘).combobox(‘setValue‘, row.zone_id); AjaxForDepartment(row.zone_id); $(‘#department_parent‘).combotree(‘setValue‘, row.id);
The above logic is very simple, that is, to load provinces, cities, and counties on the page, load province and county levels, load province levels, and load departments.
But the problem is that when I click, the province, city, county, and region levels can be loaded normally, but when I load the data to combotree, the box will be blank after the correct value is flashed.
At that time, I had no idea what the problem was. I did not see this situation in my online search. Even if you try to change row. ID to a common number, it still does not work.
Since there is no foresight, you can only analyze it yourself.
Since the combotree correctly loads the drop-down list during loading, the ajaxfordepartment method is called successfully.
But when it is loaded to setvalue, it will flash and disappear. It indicates that the problem lies here. It is likely to be related to the loading sequence or loading duration.
Why?
Javascript execution is a single thread, but we cannot know the execution sequence within the combotree, so it is very likely that
$ ('# Department_parent'). combotree ('setvalue', row. ID );
First, and
Ajaxfordepartment (row. zone_id); will be executed later, which will cause the above problems.
With a try, change the Code as follows:
AjaxForProvince(); $(‘#province‘).combobox(‘setValue‘, row.province_id); AjaxForCity(row.province_id); $(‘#city‘).combobox(‘setValue‘, row.city_id); AjaxForZone(row.city_id); $(‘#zone‘).combobox(‘setValue‘, row.zone_id); AjaxForDepartment(row.zone_id); setTimeout(function () { setTreeValue(row) }, 300); var setTreeValue = function (row) { $(‘#department_parent‘).combotree(‘setValue‘, row.id); }
Then run. Everything is normal.