jquery 表單取值常用代碼
jquery操作表單元素代碼
/*
假設(shè)在一個(gè)表單中有一個(gè)按鈕id="save"
$(document).ready(function(){
$("#save").click(function(){
$("#save").attr("disabled",true);//設(shè)為不可用
$("#form1")[0].submit();//如果你有很多個(gè)id為form1的表單也沒關(guān)系,只有第一個(gè)會提交的哈哈.
});
});
取下拉菜單選中項(xiàng)的文本;
獲取和設(shè)置下拉菜單的值;
清空下拉菜單;
給下列菜單添加元素;
取單選框值;
單選或復(fù)選按鈕的選擇;
取復(fù)選框值;
判斷單選或復(fù)選框是否被選中;
元素可用不可用;
判斷元素可用不可用。
1.取下拉菜單選中項(xiàng)的文本
$("#select option[selected]").text();//select和option之間有空格,option為select的子元素
$("#select option:selected").text();//如果寫成$("#select").text();會把所有下拉菜單的文本選擇出來
2.獲取和設(shè)置下拉菜單的值
$("#select").val();//取值
$("#select").val("value");//設(shè)置,如果select中有值為value的選項(xiàng),該選項(xiàng)就會被選中,如果不存在,則select不做任何變動
3.清空下拉菜單
$("#select").empty();
$("#select").html("");
4.給下列菜單添加元素
$('<option value="1">1</option>').appendto($("#select"));
$("#select").append('<option value="1">1</option>');
5.取單選框值
$("#id[checked]").val();
6.單選或復(fù)選按鈕的選擇
$("#id[value=val]").attr("checked",true);//選擇
$("#id[value=val]").attr("checked","");//取消選擇
$("#id[value=val]").attr("checked",false);//取消選擇
$("#id[value=val]").removeattr("checked");//取消選擇
7.取復(fù)選框值
$("input[type=checkbox][checked]").each(function(){
alert($(this).val());
})
//如果用$("input[type=checkbox][checked]").val(),只會返回第一個(gè)被選中的值
8.判斷單選或復(fù)選框是否被選中
if($("#id").attr("checked")){}//判斷選中
if($("#id").attr("checked")==true){}//判斷選中
if($("#id").attr("checked")==undefined){}//判斷未選中
9.元素可用不可用
$("#id").attr("disabled",false);//設(shè)為可用
$("#id").attr("disabled",true);//設(shè)為不可用
10.判斷元素可用不可用
if($("#id").attr("disabled")){}//判斷不可用
if($("#id").attr("disabled")==undefined){}//判斷可用
文本框操作
取 值:var textval = $("#text_id").attr("value");
var textval = $("#text_id").val();
清除內(nèi)容:$(”#txt”).attr(”value”,”);
填充內(nèi)容:$(”#txt”).attr(”value”,'123′);
文本域操作
取 值:var textval = $("#text_id").attr("value");
var textval = $("#text_id").val();
清除內(nèi)容:$(”#txt”).attr(”value”,”);
填充內(nèi)容:$(”#txt”).attr(”value”,'123′);
單選按鈕操作
取 值:var valradio = $("input[@type=radio][@checked]").val(); //只有一組Radio情況下
var valradio =$('input[@name=chart][@checked]').val(); //多組Radio情況下,根據(jù)name取一組的值
下拉框操作
取 值:var selectval = $('#sell').val();
設(shè)置選中:$("#select_id").attr("value",'test');//設(shè)置value=test的項(xiàng)目為當(dāng)前選中項(xiàng)
添加新項(xiàng):$("<option value='test'>test</option><option value='test2'>test2</option>").appendTo("#select_id")//添加下拉框的option
清空下拉框:$("#select_id").empty();//清空下拉框
多選框操作
取 值:$("#chk_id").attr("checked",'');//未選中的值
$("#chk_id").attr("checked",true);//選中的值
if($("#chk_id").attr('checked')==undefined) //判斷是否已經(jīng)選中
相關(guān)文章
原生Ajax 和jQuery Ajax的區(qū)別示例分析
這篇文章主要介紹了原生Ajax 和Jq Ajax的區(qū)別示例分析,需要的朋友可以參考下2014-12-12
jQuery插件實(shí)現(xiàn)非常實(shí)用的tab欄切換功能【案例】
這篇文章主要介紹了jQuery插件實(shí)現(xiàn)非常實(shí)用的tab欄切換功能,涉及jQuery事件響應(yīng)及頁面元素屬性動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-02-02
jquery把int類型轉(zhuǎn)換成字符串類型的方法
下面小編就為大家?guī)硪黄猨query把int類型轉(zhuǎn)換成字符串類型的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
使用jQuery不判斷瀏覽器高度解決iframe自適應(yīng)高度問題
這篇文章主要介紹了使用jQuery不判斷瀏覽器高度解決iframe自適應(yīng)高度問題,需要的朋友可以參考下2014-12-12

