一、文本框、單選按鈕、復(fù)選框、相關(guān)操作
或者對(duì)單選框默認(rèn)選定設(shè)置:
$("input[name='sex']").each(function(){
if($(this).val()==s){
$(this).attr("checked",true);
//this.checked=true;
}
});
jQuery設(shè)置Select選擇的Text和Value:
語(yǔ)法解釋:
$("#select_id ").get(0).selectedIndex=1; //設(shè)置Select索引值為1的項(xiàng)選中
$("#select_id ").val(4); //設(shè)置Select的Value值為4的項(xiàng)選中
$("#select_id option[text='jQuery']").attr("selected", true); //設(shè)置 Select的Text值為jQuery的項(xiàng)選中
jQuery添加/刪除Select的Option項(xiàng):
語(yǔ)法解釋:
$("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個(gè)Option(下拉項(xiàng))
$("#select_id").prepend("<option value='0'>請(qǐng)選擇</option>"); //為Select插入一個(gè)Option(第一個(gè)位置)
$("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個(gè))
$("#select_id option[index='0']").remove(); //刪除Select中索引值為0的 Option(第一個(gè))
$("#select_id option[value='3']").remove(); //刪除Select中Value='3'的 Option
$("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
$("#grade option[value='0']").attr("selected","selected");//默認(rèn)第一項(xiàng)被選中
})
//為Select添加事件,當(dāng)選擇其中一項(xiàng)時(shí)觸發(fā)
function showIt(){
var selectText = $("#grade option:selected").text();//獲取Select選擇的Text
//var selectText = $("#grade").find("option:selected").text();//這種方式也可行
var selectValue = $("#grade").val();//獲取被選擇的value
var selectIndex = $("#grade").get(0).selectedIndex//獲取select的索引值
var text = '選擇:'+selectText+"/n";
text +='value值:'+selectValue+"/n";
text +='索引值:'+selectIndex;
$("#text").text(text);
}
</script>
</head>
<body>
<div>
<select name='grade' id='grade' onchange="showIt()"></select>
<p><textarea name='text' id='text' row='30' col='100'></textarea></p>
</div>
</body>
</html>
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注