确定是用的这个哦, 不然可往下看也没有什么意义了
突然发现如果用在省市选择发现有如下问题:
1,不能动态设置值
2,不能动态设置选中
看了好几遍API愣是米有发现有方法可以直接调用。">
Home > Web Front-end > JS Tutorial > Jquery chosen dynamic setting value example introduction_jquery

Jquery chosen dynamic setting value example introduction_jquery

WBOY
Release: 2016-05-16 17:26:08
Original
1739 people have browsed it

Jquery chosen address: https://github.com/harvesthq/chosen
A selection plug-in that supports retrieval and multiple selection, but does not support input. The effect is as shown below
Jquery chosen dynamic setting value example introduction_jquery
I am sure to use this one Oh, otherwise there is no point in looking down
Suddenly I found the following problems if used in province and city selection:
1, the value cannot be set dynamically
2, the selection cannot be set dynamically
Look After searching the API several times, Mi found a method that could be called directly. I have no choice but to look at the source code and find that there is a judgment during its initialization as shown below:
Jquery chosen dynamic setting value example introduction_jquery
If there is a destroy, it is ("#city).chosen("destroy"); and that's it. . I tried my guess and it worked. I tried to remove the generated DOM before debugging it, but it turned out that it was not feasible. It could be removed, but it would no longer be able to be initialized. It took two or three hours.
First solve the first problem

Copy the code The code is as follows:

$("#province").change(function(){
$("#city").html( "");
$("#city").chosen("destroy");
$(""). appendTo("#city");
$.getJSON("${ctx}/api/getCityByProvinceJson.jsp",{
province:$("#province").val()
}, function(data){

$.each(data,function(i){
$("").appendTo("#city");
});
$("#city").attr("multiple",true);

$("#city").chosen({
no_results_text : "This option was not found!",
width: "70%"
});
});
} );
$("#city").chosen({
no_results_text : "This option was not found!",
width: "70%"
});

In this way, the value can be set dynamically. I feel that the code is a bit repetitive, but I haven’t found a better method yet.
The second problem is solved below.
The usage is similar to the above. First set the value for select and then initialize it. Drop-down box, the code is as follows:
Copy code The code is as follows:

$("#city").val("Shanghai");
$("#city").chosen({
no_results_text : "Not found This option!",
width:"70%"
});

This way you can dynamically set the selected
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template