Home > Article > Web Front-end > jquery operation select (value, set selected)
Every time I operate a select, I always have to go out and look up the information. Why not summarize it myself, and I will look here later.
For examplef47b572de00409da02ec5e7a3675b5d318bb6ffaf0152bbe49cd8a3620346341
1. Set the value to pxx to select the item
$(".selector") .val("pxx");
2. Set text to pxx and select the item
$(".selector").find("option[text='pxx']") .attr("selected",true);
There is a usage of square brackets. The equal sign in the square brackets is preceded by attribute name, no need to add quotation marks. Many times, the use of square brackets can make logic very simple.
3. Get the value of the currently selected item
$(".selector").val();
4. Get the text of the currently selected item
$(".selector").find("option:selected").text();
The colon is used here. Mastering its usage and drawing inferences will also make the code simpler.
The cascade of selects is often used, that is, the value of the second select changes with the value selected by the first select. This is very simple in jquery.
Such as:
$(".selector1").change(function(){ // 先清空第二个 $(".selector2").empty(); // 实际的应用中,这里的option一般都是用循环生成多个了 var option = $("<option>").val(1).text("pxx"); $(".selector2").append(option); });
The above is the detailed content of jquery operation select (value, set selected). For more information, please follow other related articles on the PHP Chinese website!