jquery 操作checkbox、radio、select 小结
jquery radio的操作:
<code class="html"><input type="radio" name="rd" checked id="rd1" value="rd1"> <input type="radio" name="rd" id="rd2" value="rd2"> <input type="radio" name="rd" id="rd3" value="rd3"></code>
1.获取选中值,三种方法都可以:
<code class="javascript">$('input:radio:checked').val(); $("input[type='radio']:checked").val(); $("input[name='rd']:checked").val(); </code>
2.设置第一个Radio为选中值:
<code class="javascript">$('input:radio:first').attr('checked', 'checked'); 或者 $('input:radio:first').attr('checked', 'true');</code>
注: attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)
3.设置最后一个Radio为选中值:
<code class="javascript">$('input:radio:last').attr('checked', 'checked'); 或者 $('input:radio:last').attr('checked', 'true'); </code>
4.根据索引值设置任意一个radio为选中值:
<code class="javascript">$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2.... 或者 $('input:radio').slice(1,2).attr('checked', 'true'); </code>
5.根据Value值设置Radio为选中值
<code class="javascript">$("input:radio[value='rd2']").attr('checked','true'); 或者 $("input[value='rd2']").attr('checked','true'); </code>
6.删除Value值为rd2的Radio
<code class="javascript">$("input:radio[value='rd2']").remove(); </code>
7.删除第几个Radio
<code class="javascript">$("input:radio").eq(索引值).remove();索引值=0,1,2.... 如删除第3个Radio:$("input:radio").eq(2).remove(); </code>
8.遍历Radio
<code class="javascript">$('input:radio').each(function(index,domEle){ //写入代码 }); </code>
jquery select的操作
<code class="html"><select id="sel"> <option value="1" selected>a</option> <option value="2">b</option> <option value="3">c</option> <option value="4">d</option> <option value="5">e</option> </select></code>
1. 获取选中项:
获取选中项的Value值:
<code class="javascript">$('select#sel option:selected').val(); 或者 $('select#sel').find('option:selected').val(); </code>
获取选中项的Text值:
<code class="javascript">$('select#seloption:selected').text(); 或者 $('select#sel').find('option:selected').text();</code>
2. 获取当前选中项的索引值:
<code class="javascript">$('select#sel').get(0).selectedIndex; </code>
3. 获取当前option的最大索引值:
<code class="javascript">$('select#sel option:last').attr("index") </code>
4. 获取DropdownList的长度:
<code class="javascript">$('select#sel')[0].options.length; 或者 $('select#sel').get(0).options.length; </code>
5. 设置第一个option为选中值:
<code class="javascript">$('select#sel option:first').attr('selected','true') 或者 $('select#sel')[0].selectedIndex = 0; </code>
6. 设置最后一个option为选中值:
<code class="javascript">$('select#sel option:last).attr('selected','true') </code>
7. 根据索引值设置任意一个option为选中值:
<code class="javascript">$('select#sel')[0].selectedIndex =索引值;索引值=0,1,2.... </code>
8. 设置Value=4 的option为选中值:
<code class="javascript">$('select#sel').attr('value','4'); 或者 $("select#sel option[value='4']").attr('selected', 'true'); </code>
9. 删除Value=3的option:
<code class="javascript">$("select#sel option[value='3']").remove(); </code>
10.删除第几个option:
<code class="javascript">$(" select#sel option ").eq(索引值).remove();索引值=0,1,2.... 如删除第3个Radio: $(" select#sel option ").eq(2).remove(); </code>
11.删除第一个option:
<code class="javascript">$(" select#sel option ").eq(0).remove(); 或者 $("select#sel option:first").remove();</code>
12. 删除最后一个option:
<code class="javascript">$("select#sel option:last").remove(); </code>
13. 删除dropdownlist:
<code class="javascript">$("select#sel").remove(); </code>
14.在select后面添加一个option:
<code class="javascript">$("select#sel").append("<option value="6">f</option>");</code>
15. 在select前面添加一个option:
<code class="javascript">$("select#sel").prepend("<option value="0">0</option>"); </code>
16. 遍历option:
<code class="javascript">$(' select#sel option ').each(function (index, domEle) { //写入代码 }); </code>
jquery checkbox的操作
<code class="html"><input type="checkbox" name="ck1" checked id="ck1" value="1"> <input type="checkbox" name="ck2" id="ck2" value="2"> <input type="checkbox" name="ck3" id="ck3" value="3"> <input type="checkbox" name="ck4" id="ck4" value="4"></code>
1. 获取单个checkbox选中项(三种写法):
<code class="javascript">$("input:checkbox:checked").val() 或者 $("input:[type='checkbox']:checked").val(); 或者 $("input:[name='ck']:checked").val(); </code>
2. 获取多个checkbox选中项:
<code class="javascript">$('input:checkbox').each(function() { if ($(this).attr('checked') ==true) { alert($(this).val()); } }); </code>
3. 设置第一个checkbox 为选中值:
<code class="javascript">$('input:checkbox:first').attr("checked",'checked'); 或者 $('input:checkbox').eq(0).attr("checked",'true'); </code>
4. 设置最后一个checkbox为选中值:
<code class="javascript">$('input:radio:last').attr('checked', 'checked'); 或者 $('input:radio:last').attr('checked', 'true'); </code>
5. 根据索引值设置任意一个checkbox为选中值:
<code class="javascript">$('input:checkbox).eq(索引值).attr('checked', 'true');索引值=0,1,2.... 或者 $('input:radio').slice(1,2).attr('checked', 'true');</code>
6. 选中多个checkbox:
同时选中第1个和第2个的checkbox:
<code class="javascript">$('input:radio').slice(0,2).attr('checked','true'); </code>
7. 根据Value值设置checkbox为选中值:
<code class="javascript">$("input:checkbox[value='1']").attr('checked','true'); </code>
8. 删除Value=1的checkbox:
<code class="javascript">$("input:checkbox[value='1']").remove(); </code>
9. 删除第几个checkbox:
<code class="javascript">$("input:checkbox").eq(索引值).remove();索引值=0,1,2.... 如删除第3个checkbox: $("input:checkbox").eq(2).remove(); </code>
10.遍历checkbox:
<code class="javascript">$('input:checkbox').each(function (index, domEle) { //写入代码 }); </code>
11.全部选中
<code class="javascript">$('input:checkbox').each(function() { $(this).attr('checked', true); }); </code>
12.全部取消选择
<code class="javascript">$('input:checkbox').each(function () { $(this).attr('checked',false); });</code>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

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

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

Dreamweaver CS6
視覺化網頁開發工具

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

jQuery引用方法詳解:快速上手指南jQuery是一個受歡迎的JavaScript庫,被廣泛用於網站開發中,它簡化了JavaScript編程,並為開發者提供了豐富的功能和特性。本文將詳細介紹jQuery的引用方法,並提供具體的程式碼範例,幫助讀者快速上手。引入jQuery首先,我們需要在HTML檔案中引入jQuery函式庫。可以透過CDN連結的方式引入,也可以下載

jQuery中如何使用PUT請求方式?在jQuery中,發送PUT請求的方法與發送其他類型的請求類似,但需要注意一些細節和參數設定。 PUT請求通常用於更新資源,例如更新資料庫中的資料或更新伺服器上的檔案。以下是在jQuery中使用PUT請求方式的具體程式碼範例。首先,確保引入了jQuery庫文件,然後可以透過以下方式發送PUT請求:$.ajax({u

jQuery如何移除元素的height屬性?在前端開發中,經常會遇到需要操作元素的高度屬性的需求。有時候,我們可能需要動態改變元素的高度,而有時候又需要移除元素的高度屬性。本文將介紹如何使用jQuery來移除元素的高度屬性,並提供具體的程式碼範例。在使用jQuery操作高度屬性之前,我們首先需要了解CSS中的height屬性。 height屬性用於設定元素的高度

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:<

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

美國太空總署透過最近的火箭發射證實了地球周圍存在全球電場。這是一件大事,因為這是對長期理論化但從未被觀察到的現象的首次直接測量

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

jQuery是一種流行的JavaScript庫,被廣泛用於處理網頁中的DOM操作和事件處理。在jQuery中,eq()方法是用來選擇指定索引位置的元素的方法,具體使用方法和應用場景如下。在jQuery中,eq()方法選擇指定索引位置的元素。索引位置從0開始計數,即第一個元素的索引是0,第二個元素的索引是1,依此類推。 eq()方法的語法如下:$("s
