Home  >  Article  >  Web Front-end  >  jquery implements dynamic operation select_jquery

jquery implements dynamic operation select_jquery

WBOY
WBOYOriginal
2016-05-16 16:14:29896browse

Today, because I have a project that requires dynamic operation of select selection habits, I searched it on Baidu, and there were quite a lot of results. I tried one of them and found that it could not be used. I opened the second and third ones and found that they were all the same, and then I did some research on my own. Just a moment

Copy code The code is as follows:

//Initialize select, the first one is selected
                 $('#stoStoreCategoryID').get(0).selectedIndex=0;
                $("#stoStoreCategoryID option").each(function(i,n){
If($(n).text()==data.scName)
                                                {
$(n).attr("selected",true);
                }
             })

I just need to judge whether the text is the same. If I want to judge whether the values ​​are the same

Copy code The code is as follows:

if($(n).val()==data.scName)

That’s it. I saw this code on the Internet

Copy code The code is as follows:

//Set the item with text as pxx selected
$(".selector").find("option[text='pxx']").attr("selected",true);

It feels feasible, but the operation was not successful

What I wrote is relatively simple. I wasn’t planning to post it at first, but I spent a lot of time on it. I sent it to others and also kept it for myself.

Statement:
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