(持續更新ing)前端--jquery--JS--常見用法與場景

最新更新時間:2020-01-19 11:13

.
.
.

2.獲取radio的選中值

jQuery中 :

var addressID = $("input[name='address']:checked").val();

詳細介紹請參考:https://blog.csdn.net/qq_39822451/article/details/82753282
.
js中:

function getValue(){
    // method 1 
	var radio = document.getElementsByName("DoorCt");
	for (i=0; i<radio.length; i++) {
		if (radio[i].checked) {
			alert(radio[i].value)
		}
	}
}

詳細介紹請參考:https://blog.csdn.net/magi1201/article/details/44465557
.
.
.

1.jquery獲取select選中的文本與值

詳細介紹請參考:https://segmentfault.com/a/1190000016816133

獲取select :

獲取select 選中的 text :

$("#ddlregtype").find("option:selected").text();

獲取select選中的 value:

$("#ddlregtype ").val();

獲取select選中的索引:

$("#ddlregtype ").get(0).selectedindex;

設置select:

設置select 選中的索引:

$("#ddlregtype ").get(0).selectedindex=index;//index爲索引值

設置select 選中的value:

  $("#ddlregtype ").attr("value","normal“);

    $("#ddlregtype ").val("normal");

    $("#ddlregtype ").get(0).value = value;
設置select 選中的text:

var count=KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲ddlregtype opti…("#ddlregtype “).get(0).options[i].text == text)
{
$(”#ddlregtype “).get(0).options[i].selected = true;
break;
}
}
$(”#select_id option[text=‘jquery’]").attr(“selected”, true);

設置select option項:

$("#select_id").append("<option value='value'>text</option>");  //添加一項option
$("#select_id").prepend("<option value='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();

工作需要,要獲得兩個表單中的值。如圖:

如何獲得從左邊選擇框添加到右邊選擇框中的值?我想了想用網頁特效可以獲得,這裏用了比較流行的jquery。
js代碼如下:

值得注意的是,不能直接寫成

$(function(){
$(’#select2’).each( //獲得select1的所有值,因爲前面講選項從左邊添加到右邊,jquery其實並沒有真正將值從左邊傳到右邊。
function(){
KaTeX parse error: Expected '}', got 'EOF' at end of input: … alert((this).val()); //獲得select2中的select1值
});
});
})

html代碼:

選項1 選項2 選項3 選項4 選項5 選項6 選項7
選中添加到右邊>> 全部添加到右邊>>
<<選中刪除到左邊 <<全部刪除到左邊

使用JQuery,Ajax調用動態填充Select的option選項

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章