溫馨提示×

JQuery怎么選中select組件的值

小億
128
2023-08-15 18:14:27
欄目: 編程語言

要選中select組件的值,可以使用以下的jQuery方法:

  1. 使用val()方法:
// 選中第一個option
$("#selectId").val($("#selectId option:first").val());
// 選中指定的value值
$("#selectId").val("value");
  1. 使用prop()方法:
// 選中第一個option
$("#selectId option:first").prop("selected", true);
// 選中指定的value值
$("#selectId option[value='value']").prop("selected", true);
  1. 使用attr()方法:
// 選中第一個option
$("#selectId option:first").attr("selected", true);
// 選中指定的value值
$("#selectId option[value='value']").attr("selected", true);

以上方法都可以選中select組件的值,只需要根據實際情況選擇適合自己的方法即可。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女