on()观察输入的变化(on() to watch changes in input)
$("#myTextBox").on("change paste keyup", function() {
alert($(this).val());
});
如何比较用户插入的新值和输入中已存在的当前值?
$("#myTextBox").on("change paste keyup", function() { alert($(this).val()); });How can I compare the new value inserted by the user and the current value that already existed in the input?
最满意答案
奇怪的是,输入的value属性在其值更改时实际上并未更新。 因此,您可以简单地将.val()与attr("value")并在进行更改后更新attr("value") :
例如:
$("#myTextBox").on("change paste keyup", function() { alert($(this).attr("value")+" vs "+$(this).val()); $(this).attr("value",$(this).val()); });<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="myTextBox" value="abc" />Weirdly, the input's value attribute is not actually updated when its value is changed. Therefore you can simply compare the .val() to the attr("value") and update the attr("value") after a change is made:
E.G:
$("#myTextBox").on("change paste keyup", function() { alert($(this).attr("value")+" vs "+$(this).val()); $(this).attr("value",$(this).val()); });<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="myTextBox" value="abc" />
更多推荐
发布评论