一、文本框、單選按鈕、復選框、相關操作
或者對單選框默認選定設置:
$("input[name='sex']").each(function(){
if($(this).val()==s){
$(this).attr("checked",true);
//this.checked=true;
}
});
jQuery設置Select選擇的Text和Value:
語法解釋:
$("#select_id ").get(0).selectedIndex=1; //設置Select索引值為1的項選中
$("#select_id ").val(4); //設置Select的Value值為4的項選中
$("#select_id option[text='jQuery']").attr("selected", true); //設置 Select的Text值為jQuery的項選中
jQuery添加/刪除Select的Option項:
語法解釋:
$("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項)
$("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select插入一個Option(第一個位置)
$("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個)
$("#select_id option[index='0']").remove(); //刪除Select中索引值為0的 Option(第一個)
$("#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");//默認第一項被選中
})
//為Select添加事件,當選擇其中一項時觸發
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>
新聞熱點
疑難解答