jQuery对select的基本操作

    xiaoxiao2021-03-25  121

    一、基本操作 为select添加事件,当选择其中一项时触发 $("#select_id").change(function(){ });    获取select选择的Text var checkText=$("#select_id").find("option:selected").text(); 获取Select选择的Value var checkValue=$("#select_id").val(); 获取select选择的索引值 var checkIndex=$("#select_id ").get(0).selectedIndex;   获取select最大的索引值 var maxIndex=$("#select_id option:last").attr("index");   设置Select索引值为1的项选中 $("#select_id ").get(0).selectedIndex=1; 设置Select的Value值为4的项选中 $("#select_id ").val(4); 设置Select的Text值为jQuery的项选中 $("#select_id option[text='jQuery']").attr("selected", true); 二、添加/删除select元素的Option项 为select追加一个Option(下拉项) $("#select_id").append(" Text"); 为select插入一个Option(第一个位置) $("#select_id").prepend(" 请选择"); 删除Select中索引值最大Option(最后一个) $("#select_id option:last").remove(); 删除Select中索引值为0的Option(第一个) $("#select_id option[index='0']").remove(); 删除Select中Value='3'的Option $("#select_id option[value='3']").remove(); 删除Select中Text='4'的Option $("#select_id option[text='4']").remove();
    转载请注明原文地址: https://ju.6miu.com/read-11667.html

    最新回复(0)