jquery操作select option 的代碼小結
更新時間:2011年06月21日 23:28:18 作者:
jquery操作select option 的代碼小結,需要的朋友可以參考下。
1、獲取選中select的value和text,html代碼如下:
<select id="mySelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
則可通過以下script代碼s來獲取選中的value和text
$("#mySelect").val(); //獲取選中記錄的value值
$("#mySelect option:selected").text(); //獲取選中記錄的text值
2、運用new Option("文本","值")方法添加選項option
var obj = document.getElementById("mySelect");
obj.add(new Option("4","4"));
3、刪除所有選項option
var obj = document.getElementById("mySelect");
obj.options.length = 0;
4、刪除選中選項option
var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
obj.options.remove(index);
5、修改選中選項option
var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
obj.options[index] = new Option("three",3); //更改對應的值
obj.options[index].selected = true; //保持選中狀態(tài)
6、刪除select
var obj = document.getElementById("mySelect");
obj.parentNode.removeChild(obj); //移除當前對象
7、select選擇的響應事件
$("#mySelect").change(function(){
//添加所需要執(zhí)行的操作代碼
})
復制代碼 代碼如下:
<select id="mySelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
則可通過以下script代碼s來獲取選中的value和text
復制代碼 代碼如下:
$("#mySelect").val(); //獲取選中記錄的value值
$("#mySelect option:selected").text(); //獲取選中記錄的text值
2、運用new Option("文本","值")方法添加選項option
復制代碼 代碼如下:
var obj = document.getElementById("mySelect");
obj.add(new Option("4","4"));
3、刪除所有選項option
復制代碼 代碼如下:
var obj = document.getElementById("mySelect");
obj.options.length = 0;
4、刪除選中選項option
復制代碼 代碼如下:
var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
obj.options.remove(index);
5、修改選中選項option
復制代碼 代碼如下:
var obj = document.getElementById("mySelect");
var index = obj.selectedIndex;
obj.options[index] = new Option("three",3); //更改對應的值
obj.options[index].selected = true; //保持選中狀態(tài)
6、刪除select
復制代碼 代碼如下:
var obj = document.getElementById("mySelect");
obj.parentNode.removeChild(obj); //移除當前對象
7、select選擇的響應事件
復制代碼 代碼如下:
$("#mySelect").change(function(){
//添加所需要執(zhí)行的操作代碼
})
您可能感興趣的文章:
- jquery操作select詳解(取值,設置選中)
- jquery實現(xiàn)動態(tài)操作select選中
- jQuery操作select下拉框的text值和value值的方法
- 利用jquery操作select下拉列表框的代碼
- jQuery操作Select的Option上下移動及移除添加等等
- jQuery操作Select選擇的Text和Value(獲取/設置/添加/刪除)
- jquery操作select大全
- jQuery操作select的實例代碼
- jquery操作select元素和option的實例代碼
- jquery下拉select控件操作方法分享(jquery操作select)
- jquery操作select常見方法大全【7種情況】
相關文章
Easyui Datagrid自定義按鈕列(最后面的操作列)
做項目的時候因為需求,要在表格的最后添加一列操作列,easyUI貌似沒有提供這種功能,下面我們來自定義按鈕列,具體實現(xiàn)代碼,大家參考下本文吧2017-07-07Tab頁界面 用jQuery及Ajax技術實現(xiàn)(php后臺)
到了B/S開發(fā)時代,網(wǎng)頁前端布局也把Tab頁的布局形式吸收了過來。特別是和Ajax技術結合起來,可以更充分發(fā)揮Tab頁的良好表現(xiàn)力和數(shù)據(jù)緩存的優(yōu)勢,是一種良好的網(wǎng)頁布局形式2011-10-10使用基于jquery的gamequery插件做JS乒乓球游戲
現(xiàn)在jquery比較流行,用js做游戲的也越來越多了,雖然現(xiàn)在html5出來了,但實際上要用html5做點啥出來還是得靠javascript,所以學好js是非常重要的2011-07-07