jQuery取得select选择的文本与值
生活随笔
收集整理的這篇文章主要介紹了
jQuery取得select选择的文本与值
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
jquery獲取select選擇的文本與值
獲取select :
獲取select 選中的 text :
? ??$("#ddlregtype").find("option:selected").text();
獲取select選中的 value:
???$("#ddlregtype ").val();
獲取select選中的索引:
???$("#ddlregtype ").get(0).selectedindex;
設(shè)置select:
設(shè)置select 選中的索引:
???$("#ddlregtype ").get(0).selectedindex=index;//index為索引值
設(shè)置select 選中的value:
???$("#ddlregtype ").attr("value","normal“);
???$("#ddlregtype ").val("normal");
???$("#ddlregtype ").get(0).value = value;
設(shè)置select 選中的text:
???var count=$("#ddlregtype option").length;
?????for(var i=0;i<count;i++)
????????{??????????if($("#ddlregtype ").get(0).options[i].text == text)
???????????{
???????????????$("#ddlregtype ").get(0).options[i].selected = true;
???????????????break;
???????????}
???????}
???$("#select_id option[text='jquery']").attr("selected", true);
設(shè)置select option項:
???$("#select_id").append("<optionvalue='value'>text</option>");?//添加一項option
???$("#select_id").prepend("<optionvalue='0'>請選擇</option>");//在前面插入一項option
???$("#select_id option:last").remove(); //刪除索引值最大的option
???$("#select_id option[index='0']").remove();//刪除索引值為0的option
???$("#select_id option[value='3']").remove(); //刪除值為3的option
???$("#select_id option[text='4']").remove(); //刪除text值為4的option
清空 select:
???$("#ddlregtype ").empty();
工作需要,要獲得兩個表單中的值。如圖:
如何獲得從左邊選擇框添加到右邊選擇框中的值?我想了想用網(wǎng)頁特效可以獲得,這里用了比較流行的jquery。
js代碼如下:
???//獲取所有屬性值 var item = $("#select1").val();
???$(function(){
?????$('#select1').each(? //獲得select1的所有值
????????function(){
???????????$('button').click(function(){
???????????????alert($('#select2').val());?//獲得select2中的select1值
???????????});
????????});
???})
???</script>
值得注意的是,不能直接寫成
???$(function(){
?????$('#select2').each(?//獲得select1的所有值,因為前面講選項從左邊添加到右邊,jquery其實并沒有真正將值從左邊傳到右邊。
????????function(){
???????????$('button').click(function(){
???????????????alert($(this).val());?//獲得select2中的select1值
???????????});
????????});
???})
html:
???<div class="centent">
???????????<select multiple="multiple" id="select1" name="dd"style="width:100px;height:160px;">
???????????????<optionvalue="1">選項1</option>
???????????????<optionvalue="2">選項2</option>
???????????????<optionvalue="3">選項3</option>
???????????????<optionvalue="4">選項4</option>
???????????????<optionvalue="5">選項5</option>
???????????????<optionvalue="6">選項6</option>
???????????????<optionvalue="7">選項7</option>
???????????</select>
???????????<div>
???????????????<span id="add">選中添加到右邊>></span>
???????????????<span id="add_all">全部添加到右邊>></span>
???????????</div>
???????</div>
???????<div class="centent">
???????????<select multiple="multiple" id="select2" name="sel"style="width: 100px;height:160px;">
???????????</select>
???????????<div>
???????????????<spanid="remove"><<選中刪除到左邊</span>
???????????????<spanid="remove_all"><<全部刪除到左邊</span>
???????????</div>
???????</div>
使用JQuery,Ajax調(diào)用動態(tài)填充Select的option選項
???//綁定ClassLevel1單擊事件
??????$("#ClassLevel1").change(function () {
??????????var id = $("#ClassLevel1").val();
??????????var level2 = $("#ClassLevel2");
??????????level2.empty();
??????????$("#ClassLevel3").hide();
??????????$.ajax({
??????????????url: "./askCommon.ashx?action=getclasslevel&pid=" +id,
??????????????data: { "type": "ajax" },
??????????????datatype: "json",
??????????????type: "get",
??????????????success: function (data) {
??????????????????var json = eval_r(data);
??????????????????for (var ind in json) {
??????????????????????level2.append($("<option value='" + json[ind].id +"'>" + json[ind].typename +"</option>"));
??????????????????}
???
??????????????}
??????????});
??????})
獲取select :
獲取select 選中的 text :
? ??$("#ddlregtype").find("option:selected").text();
獲取select選中的 value:
???$("#ddlregtype ").val();
獲取select選中的索引:
???$("#ddlregtype ").get(0).selectedindex;
設(shè)置select:
設(shè)置select 選中的索引:
???$("#ddlregtype ").get(0).selectedindex=index;//index為索引值
設(shè)置select 選中的value:
???$("#ddlregtype ").attr("value","normal“);
???$("#ddlregtype ").val("normal");
???$("#ddlregtype ").get(0).value = value;
設(shè)置select 選中的text:
???var count=$("#ddlregtype option").length;
?????for(var i=0;i<count;i++)
????????{??????????if($("#ddlregtype ").get(0).options[i].text == text)
???????????{
???????????????$("#ddlregtype ").get(0).options[i].selected = true;
???????????????break;
???????????}
???????}
???$("#select_id option[text='jquery']").attr("selected", true);
設(shè)置select option項:
???$("#select_id").append("<optionvalue='value'>text</option>");?//添加一項option
???$("#select_id").prepend("<optionvalue='0'>請選擇</option>");//在前面插入一項option
???$("#select_id option:last").remove(); //刪除索引值最大的option
???$("#select_id option[index='0']").remove();//刪除索引值為0的option
???$("#select_id option[value='3']").remove(); //刪除值為3的option
???$("#select_id option[text='4']").remove(); //刪除text值為4的option
清空 select:
???$("#ddlregtype ").empty();
工作需要,要獲得兩個表單中的值。如圖:
如何獲得從左邊選擇框添加到右邊選擇框中的值?我想了想用網(wǎng)頁特效可以獲得,這里用了比較流行的jquery。
js代碼如下:
???//獲取所有屬性值 var item = $("#select1").val();
???$(function(){
?????$('#select1').each(? //獲得select1的所有值
????????function(){
???????????$('button').click(function(){
???????????????alert($('#select2').val());?//獲得select2中的select1值
???????????});
????????});
???})
???</script>
值得注意的是,不能直接寫成
???$(function(){
?????$('#select2').each(?//獲得select1的所有值,因為前面講選項從左邊添加到右邊,jquery其實并沒有真正將值從左邊傳到右邊。
????????function(){
???????????$('button').click(function(){
???????????????alert($(this).val());?//獲得select2中的select1值
???????????});
????????});
???})
html:
???<div class="centent">
???????????<select multiple="multiple" id="select1" name="dd"style="width:100px;height:160px;">
???????????????<optionvalue="1">選項1</option>
???????????????<optionvalue="2">選項2</option>
???????????????<optionvalue="3">選項3</option>
???????????????<optionvalue="4">選項4</option>
???????????????<optionvalue="5">選項5</option>
???????????????<optionvalue="6">選項6</option>
???????????????<optionvalue="7">選項7</option>
???????????</select>
???????????<div>
???????????????<span id="add">選中添加到右邊>></span>
???????????????<span id="add_all">全部添加到右邊>></span>
???????????</div>
???????</div>
???????<div class="centent">
???????????<select multiple="multiple" id="select2" name="sel"style="width: 100px;height:160px;">
???????????</select>
???????????<div>
???????????????<spanid="remove"><<選中刪除到左邊</span>
???????????????<spanid="remove_all"><<全部刪除到左邊</span>
???????????</div>
???????</div>
使用JQuery,Ajax調(diào)用動態(tài)填充Select的option選項
???//綁定ClassLevel1單擊事件
??????$("#ClassLevel1").change(function () {
??????????var id = $("#ClassLevel1").val();
??????????var level2 = $("#ClassLevel2");
??????????level2.empty();
??????????$("#ClassLevel3").hide();
??????????$.ajax({
??????????????url: "./askCommon.ashx?action=getclasslevel&pid=" +id,
??????????????data: { "type": "ajax" },
??????????????datatype: "json",
??????????????type: "get",
??????????????success: function (data) {
??????????????????var json = eval_r(data);
??????????????????for (var ind in json) {
??????????????????????level2.append($("<option value='" + json[ind].id +"'>" + json[ind].typename +"</option>"));
??????????????????}
???
??????????????}
??????????});
??????})
總結(jié)
以上是生活随笔為你收集整理的jQuery取得select选择的文本与值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript中判断为整数的多种方
- 下一篇: js中的触屏事件