js
jquery
一、获取select option选中
返回 string
var e = document.getElementById("sel");
var value = e.value;
var text = e.options[e.selectedIndex].text;
.
let a1 = $("#sel option:selected").val()
let a2 = $("#sel option:selected").text()
二、获取 input radio 的值
返回 string
var val = document.querySelector('input[name="genderS"]:checked').value;
.
var val = $('input[name="genderS"]:checked').val();
三、input checkbox 是否选中
返回 true false
document.querySelector('.messageCheckbox').checked;
.
let x2=$(".messageCheckbox").is(":checked")
四、input checkbox 获取全部选中
返回 [1, 2, ...]
var array = []
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')
for (var i = 0; i < checkboxes.length; i++) {
array.push(checkboxes[i].value)
}
.
let yourArray = new Array()
$("input:checkbox[name=abc]:checked").each(function () {
yourArray.push($(this).val());
});
五、遍历dom
得到 dom object
const divs = document.getElementsByTagName('div');
for (let i = 0; i < divs.length; i++) {
const ps = divs[i].getElementsByTagName('i');
for (let j = 0; j < ps.length; j++) {
console.log(ps[j].getAttribute('class'));
}
}
.
$('.icon-list i').each((idx, dom) => {
console.log(dom)
console.log(dom.getAttribute('class'))
})