[英]jQuery with Input Type = “Number”
簡單的問題,但我正在尋找正確的方法來處理這個問題。
我把它分解為仍然顯示問題的最簡單的例子。
假設我有一個基本的HTML表格:
<table id="customerTable">
<caption class="bg-primary h4">Customer Overview</caption>
<thead>
<tr>
<th>Customer ID</th>
<th>Customer Name</th>
<th># Projects</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Enterprise Management</td>
<td>14</td>
</tr>
<tr>
etc ...
</tr>
</tbody>
</table>
然后我有一個輸入類型:
<input id="colorColumnRed" type="number" />
jQuery如下, redText
是基本的CSS,可以將顏色更改為紅色。 我正在處理change
和keyup
事件以嘗試捕獲兩種情況。
$(function () {
$('#colorColumnRed').bind('change keyup', function () {
var colId = $('#colorColumnRed').val();
$('#customerTable td:nth-child(' + colId + ')').toggleClass('redText');
});
});
問題是,當用戶在數字字段中鍵入數字時,它按預期工作。 但是,如果他們然后將焦點更改為另一個輸入,則CSS 將恢復 。
如果他們使用數字向上/向下選擇器 ,則在控件失去焦點后更改仍然存在 。
我在jQuery選擇器中缺少什么來阻止它在失去焦點時手動輸入時還原? 我已經嘗試了各種focus
事件, blur
等,但找不到改變這種行為的事件。
@Rayon Dabre很快回答了這個簡單快捷的解決方案。
$('#colorColumnRed').bind('input')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.