jquery對單選框,多選框,文字方塊等常見操作小結_javascript技巧
May 16, 2016 pm 05:04 PM一、文本框、單選按鈕、複選框、相關操作
var sex=$("input[name='sex']:checked").val(); //取得一組radio被選取項的值
var item=$("#sel option:selected").text(); //取得select被選取項目的文字
var option_num=$('#sel').val(); //取得select項目索引
$("#sel")[0].selectedIndex =1; //select找到下拉方塊的第二個元素為目前選取值
$$ ']").get(1).checked=true; //radio單選組的第二個元素為目前選取值
或預設單選框預設:
$("input[name='sex']").each(function(){
if($(this).val()= =s){
$(this).attr("checked",true);
//this.checked=true;
}
});
Jquery 根據value值設置下拉列表(select)默認選中項 代碼如下:
<script><BR> function bao(s)<BR> <script><BR> function bao(s)<BR> {<BR> txt.value=s;<BR> //選擇後,讓第一項被選中,這樣,就有Change啦.<BR> true;<BR> }</script>
二、 jQuerySelect選擇的Text和取得Select選擇的Text Value
程式碼如下:
程式碼如下:
語法解釋: $("#select_id").change(function(){//code...}); //為Select新增事件,當選擇其中一項時觸發 var checkText=$("#select_id" ).find("option:selected").text(); //取得Select選擇的Text
var checkValue=$("#select_id").val(); //取得Select選擇的Value var checkIndex=$("#select_id ").get(0).selectedIndex; //取得Select選擇的索引值
var maxIndex=$("#select_id option:last").attr("index"); //取得Select最大的索引值
語法解釋:
$("#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(""); //為Select追加一個Option(下拉項目)
應用:
複製程式碼
複製程式碼

熱門文章

熱門文章

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)