jQuery 教程:用 input 和 change 检测输入值变化
要在用户编辑时更新结果,请用 jQuery 的 .on('input', handler)。要在修改确认后处理,请用 .on('change', handler)。复选框和单选按钮也可以用 change 读取选择。示例通过两个数相乘来比较这两个时机。
选择 input 或 change
用户输入、删除或粘贴使值发生改变时会触发 input,无需等待松开按键。change 在修改确认时触发。直接键入数字时,通常在已修改的输入框失去焦点时确认。不同控件的行为见 HTML 输入事件规则。
| 目的 | 事件 | 更新时间 |
|---|---|---|
| 输入数字时计算 | input |
值变化时 |
| 确认数字后计算 | change |
修改确认时 |
| 读取复选框或单选按钮 | change |
复选框状态变化或选中另一选项时 |
把两个事件都绑定到同一操作,一次修改可能执行两遍。示例使用两个独立输出进行比较。登记方式见 jQuery .on() 文档,确认时机见 jQuery change 事件文档。
比较输入时和确认后的计算结果
将完整代码以 UTF-8 保存为 jquery-input-change.html 并在浏览器打开。示例需要联网从官方 CDN 加载 jQuery 4.1.0。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>用 jQuery 检测输入值变化</title>
<script src="https://code.jquery.com/jquery-4.1.0.min.js"></script>
<style>
body { margin: 1rem; color: #172633; font: 1rem/1.6 system-ui, sans-serif; }
main { max-width: 38rem; margin: auto; }
fieldset { min-inline-size: 0; margin-block: 1rem; border: 1px solid #708896; }
input[type="number"] { box-sizing: border-box; max-width: 100%; width: 8rem; font: inherit; }
output { font-weight: 700; }
label { cursor: pointer; }
</style>
</head>
<body>
<main>
<h1>用 jQuery 检测输入值变化</h1>
<fieldset>
<legend>两个数相乘</legend>
<p><label for="a">A</label> <input type="number" id="a" step="any"></p>
<p><label for="b">B</label> <input type="number" id="b" step="any"></p>
<p>输入时 <output id="live" for="a b" aria-live="off">—</output></p>
<p>确认后 <output id="committed" for="a b">—</output></p>
</fieldset>
<fieldset>
<legend>复选框</legend>
<label><input type="checkbox" id="option"> 启用通知</label>
<p><output id="checked" for="option">未勾选</output></p>
</fieldset>
<fieldset id="fruits">
<legend>选择水果</legend>
<label><input type="radio" name="fruit" id="apple" value="apple"> 苹果</label>
<label><input type="radio" name="fruit" id="banana" value="banana"> 香蕉</label>
<p><output id="picked" for="apple banana">—</output></p>
</fieldset>
</main>
<script>
$(function () {
function product() {
const a = document.querySelector("#a").valueAsNumber;
const b = document.querySelector("#b").valueAsNumber;
const result = a * b;
return Number.isFinite(result) ? String(result) : "—";
}
$("#a, #b")
.on("input", () => $("#live").text(product()))
.on("change", () => $("#committed").text(product()));
$("#option").on("change", function () {
$("#checked").text($(this).prop("checked")
? "已勾选" : "未勾选");
});
$("#fruits").on("change", 'input[name="fruit"]', function () {
const label = this.labels[0].textContent.trim();
$("#picked").text(label + " (" + $(this).val() + ")");
});
});
</script>
</body>
</html>
在 A 中输入 2,再到 B 输入 3。B 仍具有焦点时,即时结果为 6,确认结果为 —。按 Tab 离开 B,确认结果也变为 6。valueAsNumber 读取数字,空白或无效结果显示为 —,实际的零会得到 0。
读取复选框和单选按钮的选择
勾选和取消勾选复选框。$(this).prop('checked') 以布尔值读取当前勾选状态。.val() 读取配置好的值,无论是否勾选。
两个按钮共用 name="fruit",因此只能选择其中一个。从苹果换到香蕉时,新选中的按钮触发 change。处理函数中的 this 是该按钮,$(this).val() 读取其值,例如 banana。
在代码修改数值后更新结果
执行 .val('4') 会改变输入框的值,但不会自动触发用户输入事件。如果需要运行同样的处理函数,请在赋值后显式触发所需事件。细节见 jQuery .val() 文档。
$("#a").val("4");
$("#a").trigger("input").trigger("change");
两个结果都为 6 时,在开发者控制台只运行第一行。A 变为 4,两个结果仍是 6。运行第二行就会把两个结果更新为 12。如果页面只有一个更新函数,也可以直接调用它。
排查事件处理函数不执行的原因
没有反应时,先在网络面板确认 jQuery 已加载,再检查选择器是否匹配控件。仅对键入有效时,请检查是否绑定了 keyup。复选框应检查当前的 checked 属性。
为处理后来添加的单选按钮,示例把处理函数注册到持续存在的 #fruits 容器,并将单选按钮选择器作为第二个参数。这称为事件委托。


