jquery對單選框,多選框,文本框等常見操作小結(jié)
一、文本框、單選按鈕、復(fù)選框、相關(guān)操作
var sex=$("input[name='sex']:checked").val(); //獲取一組radio被選中項(xiàng)的值
var item=$("#sel option:selected").text(); //獲取select被選中項(xiàng)的文本
var option_num=$('#sel').val(); //獲取select項(xiàng)目索引
$("#sel")[0].selectedIndex =1; //select下拉框的第二個元素為當(dāng)前選中值
$("input[name='sex']").get(1).checked=true; //radio單選組的第二個元素為當(dāng)前選中值
或者對單選框默認(rèn)選定設(shè)置:
$("input[name='sex']").each(function(){
if($(this).val()==s){
$(this).attr("checked",true);
//this.checked=true;
}
});
Jquery 根據(jù)value值設(shè)置下拉列表(select)默認(rèn)選中項(xiàng)
<select name=sel onchange="bao(this.options[this.options.selectedIndex].value)">
<option value="">請選擇
<option value="1">Item 1
<option value="2">Item 2
<option value="3">Item 3
</select>
<script>
function bao(s)
{
txt.value=s;
//選擇后,讓第一項(xiàng)被選中,這樣,就有Change啦.
document.all.sel.options[0].selected=true;
}
</script>
<textarea id=txt></textarea>
二、 jQuery獲取 Select選擇的Text和Value
語法解釋:
$("#select_id").change(function(){//code...}); //為Select添加事件,當(dāng)選擇其中一項(xiàng) 時觸發(fā)
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最大的索引值
jQuery設(shè)置Select選擇的Text和Value:
語法解釋:
$("#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):
語法解釋:
$("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項(xiàng))
$("#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
應(yīng)用:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>jQuery common</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//初始化下拉列表--動態(tài)添加
var item = ['幼兒園','小學(xué)','初中','高中','大學(xué)','研究生','博士','碩士'];
var html ="<option value='0'>請選擇</option>";
for (var i = 0;i < item.length;i++){
html += "<option value='"+(i+1)+"'>"+item[i]+"</option>";
}
$("#grade").empty().append(html);
$("#grade option[value='0']").attr("selected","selected");//默認(rèn)第一項(xiàng)被選中
})
//為Select添加事件,當(dāng)選擇其中一項(xiàng)時觸發(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>
- jQuery 獲取多選框的值及多選框中文的函數(shù)
- jQuery實(shí)現(xiàn)的多選框多級聯(lián)動插件
- jquery select多選框的左右移動 具體實(shí)現(xiàn)代碼
- 簡單實(shí)現(xiàn)jQuery多選框功能
- jQuery對下拉框,單選框,多選框的操作
- jQuery多選框選擇數(shù)量限制方法
- jquery中表單 多選框的一種巧妙寫法
- jQuery模擬html下拉多選框的原生實(shí)現(xiàn)方法示例
- Jquery多選框互相內(nèi)容交換的實(shí)例代碼
- jQuery中實(shí)現(xiàn)prop()函數(shù)控制多選框(全選,反選)
- jQuery實(shí)現(xiàn)獲取多選框的值示例
相關(guān)文章
JavaScript實(shí)現(xiàn)簡單的文本逐字打印效果示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡單的文本逐字打印效果,涉及javascript結(jié)合時間函數(shù)動態(tài)操作頁面元素相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04
js獲取兩個數(shù)組對象的差集實(shí)現(xiàn)方法
這篇文章主要為大家介紹了js獲取兩個數(shù)組對象的差集實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
JS中ESModule和commonjs介紹及使用區(qū)別
這篇文章主要介紹了JS中ESModule和commonjs介紹及使用區(qū)別,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
用JS實(shí)現(xiàn)簡單的屏幕錄像機(jī)功能
這篇文章主要給大家介紹了如何用JS實(shí)現(xiàn)簡單的屏幕錄像機(jī),文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12
JavaScript節(jié)點(diǎn)及列表操作實(shí)例小結(jié)
這篇文章主要介紹了JavaScript節(jié)點(diǎn)及列表操作的方法,以實(shí)例的形式較為詳細(xì)的總結(jié)了javascript針對節(jié)點(diǎn)操作的相關(guān)技巧,并給出了一個完整的節(jié)點(diǎn)操作方法實(shí)例總結(jié),需要的朋友可以參考下2015-08-08
JavaScript實(shí)現(xiàn)三級聯(lián)動菜單實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)三級聯(lián)動菜單實(shí)例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
JavaScript實(shí)現(xiàn)多欄目切換效果
在網(wǎng)站開發(fā)中尤其是新聞類網(wǎng)站,經(jīng)常遇到多欄目切換的設(shè)計(jì),這種效果有很多種實(shí)現(xiàn)效果,現(xiàn)在記錄一種很簡單的寫法2016-12-12
javascript 實(shí)現(xiàn)的類似hao123的多郵箱登錄效果
javascript 實(shí)現(xiàn)的類似hao123的多郵箱登錄效果...2007-08-08

