Html+JavaScript实现表格(table)单选与多选

目录

  • 1、单选
  • 2、多选
  • 3、相关链接


1、单选

<table class="radio-table" border>
	<thead>
		<tr>
			<th>#th>
			<th>idth>
			<th>姓名th>
			<th>年龄th>
		tr>
	thead>
	<tbody>
		<tr>
			<td>
				<input type="radio" name="select" value="1" onclick="clickRadio()">
			td>
			<td>1td>
			<td>张三td>
			<td>20td>
		tr>
		<tr>
			<td>
				<input type="radio" name="select" value="2" onclick="clickRadio()">
			td>
			<td>2td>
			<td>李四td>
			<td>21td>
		tr>
		<tr>
			<td>
				<input type="radio" name="select" value="3" onclick="clickRadio()">
			td>
			<td>3td>
			<td>王五td>
			<td>24td>
		tr>
	tbody>
table>
<h3>当前选择: <span id="checked">span>h3>
<script src="./index.js">script>

function clickRadio() {
    document.getElementById('checked').innerHTML = document.querySelector('.radio-table tbody input[type=radio]:checked').value;
}

2、多选

<table class="multi-table" border>
	<thead>
		<tr>
			<th>
				<input id="js-all-checkbox" type="checkbox" onclick="checkAll(this)">
			th>
			<th>idth>
			<th>姓名th>
			<th>年龄th>
		tr>
	thead>
	<tbody>
		<tr>
			<td>
				<input type="checkbox" name="select" value="1" oninput="clickCheckbox()">
			td>
			<td>1td>
			<td>张三td>
			<td>16td>
		tr>
		<tr>
			<td>
				<input type="checkbox" name="select" value="2" oninput="clickCheckbox()">
			td>
			<td>2td>
			<td>李四td>
			<td>17td>
		tr>
		<tr>
			<td>
				<input type="checkbox" name="select" value="3" onclick="clickCheckbox()">
			td>
			<td>3td>
			<td>王五td>
			<td>19td>
		tr>
	tbody>
table>

<h3>当前选中: <span id="js-check-text">span>h3>

<script src="./index.js">script>

let checkValues = [];

function clickCheckbox(e) {
    let checkDomArr = document.querySelectorAll('.multi-table tbody input[type=checkbox]:checked');
    checkValues = [];
    for (let i = 0, len = checkDomArr.length; i < len; i++) {
        checkValues.push(checkDomArr[i].value);
    }
    updateText();
    let allCheckDomArr = document.querySelectorAll('.multi-table tbody input[type=checkbox]');
    let allCheckbox = document.getElementById('js-all-checkbox');
    for (let i = 0, len = allCheckDomArr.length; i < len; i++) {
        if (!allCheckDomArr[i].checked) {
            if (allCheckbox.checked) allCheckbox.checked = false;
            break;
        } else if (i === len - 1) {
            document.getElementById('js-all-checkbox').checked = true;
            return;
        }
    }
}

function checkAll(current) {
    let allCheckDomArr = document.querySelectorAll('.multi-table tbody input[type=checkbox]');
    if (!current.checked) { // 点击的时候, 状态已经修改, 所以没选中的时候状态时true
        checkValues = [];
        for (let i = 0, len = allCheckDomArr.length; i < len; i++) {
            let checkStatus = allCheckDomArr[i].checked;
            if (checkStatus) allCheckDomArr[i].checked = false;
        }
    } else {
        checkValues = [];
        for (let i = 0, len = allCheckDomArr.length; i < len; i++) {
            let checkStatus = allCheckDomArr[i].checked;
            if (!checkStatus) allCheckDomArr[i].checked = true;
            checkValues.push(allCheckDomArr[i].value);
        }
    }
    updateText();
}

function updateText() {
	document.getElementById('js-check-text').innerHTML = JSON.stringify(checkValues);
}

3、相关链接

简书-HTML单选表格和多选表格实现

你可能感兴趣的:(HTML,JavaScript,javascript,html,前端)