我正在構建一個Flask應用程序,而我的HTML/JavaScript/JQuery不是很好。基於複選框輸入禁用HTML輸入
比方說,我的HTML看起來像這樣:
<table class="table table-bordered table-striped table-hover" align="center" style="border-spacing: 0px">
<tr style="background-color: DodgerBlue">
<th>Task</th>
<th>Task Enabled</th>
<th>Manual Dates</th>
<th>Value Date</th>
<th>Previous Value Date</th>
</tr>
<tr>
<td>First Row</td>
<td><input type="checkbox" name="aaa1" value="true"></td>
<td><input type="checkbox" name="aaa2" value="true" onClick="myFunction()"></td>
<td><input type="date" name="aaa3" id="myCheck" disabled></td>
<td><input type="date" name="aaa4" disabled></td>
</tr>
<tr>
<td>Second Row</td>
<td><input type="checkbox" name="bbb1" value="true"></td>
<td><input type="checkbox" name="bbb2" value="true"></td>
<td><input type="date" name="bbb3"></td>
<td><input type="date" name="bbb4"></td>
</tr>
<tr>
<td>Third Row</td>
<td><input type="checkbox" name="ccc1" value="true"></td>
<td><input type="checkbox" name="ccc2" value="true"></td>
<td><input type="date" name="ccc3"></td>
<td><input type="date" name="ccc4"></td>
</tr>
</table>
我想每一行(HTML日期輸入)的第二和第三列在默認情況下被禁用。如果您在每行中勾選第二個複選框,則它將啓用兩個日期輸入,如果不勾選它,則會再次禁用兩個日期輸入。
目前,我有這個JavaScript代碼1日起投入工作,但它僅適用於第一次點擊:
<script>
function myFunction() {
document.getElementById("myCheck").disabled = false;
}
</script>
此表包含超過40行,它們都具有完全一樣的格式:
Column 1: Checkbox
Column 2: Checkbox <---- This one determines the state of both date inputs
Column 3: Date input <---- Disabled by default, checkbox determines state
Column 4: Date input <---- Disabled by default, checkbox determines state
什麼是編程控制的各行中的2日投入基礎上每行的第二個複選框的點擊活動狀態的最好方法?
編輯:
<script>
$('table tr').find(':checkbox:eq(1)').change(function() {
$(this).closest('tr').find('input[type="date"]').prop('disabled', !this.checked);
});
</script>
<script>
$('table tr').find(':checkbox:eq(0)').change(function() {
$(this).closest('tr').find(':checkbox:eq(1), input[type="date"]').prop('disabled', !this.checked);
});
</script>
太棒了!這工作。還有一件快事,看起來第一個複選框禁用/啓用其他3列是什麼樣的?我假設一個額外的功能? – Harrison
是的,這將是複選框':eq(0)' –
上類似邏輯的另一個函數如何在查找函數中包含多個輸入類型? – Harrison