2025-07-03 19:20
苹果
选择框是网页开发中常见的交互元素之一,它允许用户从一个预定义的选项列表中选择一个或多个选项。在使用jQuery进行网页开发时,我们可以通过编程方式选择选择框中的选项,以实现对选项的操作和控制。
html<select id="fruits"> <option value="Apple">苹果</option> <option value="banana">香蕉</option> <option value="orange">橙子</option></select>
Javascript$("#fruits").val("banana");
Javascript$("#fruits option").eq(1).prop("selected", true);
Javascript$("#fruits option[value='orange']").prop("selected", true);
html<!DOCTYPE html><html><head> <title>选择框操作示例</title> <script src="https://code.jquery.com/jquery-3.6.0.min.JS"></script></head><body> <select id="fruits"> <option value="Apple">苹果</option> <option value="banana">香蕉</option> <option value="orange">橙子</option> </select> <script> // 通过值选择选项 $("#fruits").val("banana"); // 通过索引选择选项 $("#fruits option").eq(1).prop("selected", true); // 通过属性选择选项 $("#fruits option[value='orange']").prop("selected", true); </script></body></html>
关于我们|免责条款|版权声明|侵权举报|隐私政策|联系我们
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号