drop-down box select uncheck question

 Recently, in the project, I encountered the problem of unchecking the drop-down box select. At that time, I directly used the attr() method in jquery

$("#type").find("option:selected").attr("selected", false);

After the code is run, it does not achieve the desired effect. Obviously this method does not work.


Then I thought of the prop() method


prop() is a built-in method in jQuery that sets or returns properties and values ​​of the selected element. When this method is used to return an attribute value, it returns the value of the FIRST matching element, and when this method is used to set an attribute value, it sets one or more attributes for the selected element

Parameters: It accepts two parameters as specified below:
            para1: It specifies the attribute.
            para2: If set, specifies the value of the attribute.
Return Value: It returns the attribute of the selected element and the value set for that attribute.

Use it directly, attach the code, I am using the first one

$("#type").find("option").each(function(){
    $(this).prop('selected', false)
})
$("#type").find("option:selected").prop("selected", false);

###### The code runs, perfect solution. Like it if you find it helpful

Guess you like

Origin blog.csdn.net/Mr_LiangDaGe/article/details/126410297