[英]change event not working for a textbox when the value is assigned externally
I have a textbox and a button. 我有一个文本框和一个按钮。 When I click the button, it sets certain value in the textbox. 单击该按钮时,它会在文本框中设置某个值。 I want to submit the page whenever the value of the textbox is changed. 我想在文本框的值发生变化时提交页面。
HTML: HTML:
<input type='text' class="set" size=50>
<input type="button" id="click" value="Click" />
JQuery: JQuery的:
$(document).ready(function () {
$("input").change(function () {
alert("Changed!");
});
$("#click").click(function () {
$('.set').val('Why am I not getting the alert saying - Changed! ');
});
});
I am able to trigger the change event when I am editing the textbox content.I know that the change event gets triggered when the textbox loses focus.This is not the case when I am changing the value by button click. 我在编辑文本框内容时能够触发更改事件。我知道当文本框失去焦点时会触发更改事件。当我通过按钮单击更改值时,情况并非如此。
I have seen answers which monitors the textbox for change at a specified time interval , say 0.1 sec. 我看到了在指定时间间隔(例如0.1秒)内监视文本框更改的答案。
Is there no other way for this to be done. 没有其他方法可以做到这一点。 Thanks. 谢谢。
EDIT - 1 * Sorry for not being clear * in my original post itself. 编辑-1 * 对不起,不清楚 *在我的原始帖子中。
My requirement is to trigger an event whenever the inputbox content is changed - Not necessarily by the click of a button.I have added a button just to explain the problem. 我的要求是每当输入框内容发生变化时触发事件 - 不一定只需点击按钮。我添加了一个按钮来解释问题。 The change may be due to any reason. 更改可能是由于任何原因。
So,the code(inside the button click function) 因此,代码(在按钮单击功能内部)
$('.set').val('Hello').change();
will not solve the problem. 不会解决问题。 Hope I am clear this time. 希望这次我很清楚。
Please suggest. 请建议。
Fire the event manually after setting the value using the change()
function. 使用change()
函数设置值后,手动触发事件。
$(document).ready(function () {
$("input").change(function () {
alert("Changed!");
});
$("#click").click(function () {
$('.set').val('Why am I not getting the alert saying - Changed! ');
$('.set').change(); //Manual fire of event.
});
});
Working Example http://jsfiddle.net/RUdu2/ 工作示例 http://jsfiddle.net/RUdu2/
Programmatically changing the value of an input doesn't fire its change event. 以编程方式更改输入的值不会触发其更改事件。 You'll have to do that yourself using .trigger('change')
(or just .change()
) at the same time as changing its value: 你必须在改变它的值的同时使用.trigger('change')
(或只是.change()
)来做这件事:
$('.set').val('Why am I not getting the alert saying - Changed! ').trigger('change');
You need to do this, when you are setting or changing the value of the text input chain it with .change()
too: 在设置或更改文本输入链的值时,也需要使用.change()
来执行此操作:
$("#click").click(function () {
$('.set').val('Why am I not getting the alert saying - Changed! ').change();
}); //-------------------------------------this one here-------------^^^^^^^^^
So whenever you set the input value this way the change event will be triggered right after click. 因此,无论何时以这种方式设置输入值,都将在单击后立即触发更改事件。
May be this could help 也许这可以帮助
$("input").on('change keyup', function () {
alert("Changed!");
});
You're changing the value of .set
programatically, so the event doesn't fire. 您正在以编程方式更改.set
的值,因此事件不会触发。 You have to trigger it manually: 您必须手动触发它:
$('.set').val('Why am I not getting the alert saying - Changed! ').change();
$(document).ready(function () {
$("input").change(function () {
Submitform();//Function for submitting form
});
$("#click").click(function () {
$('.set').val('Why am I not getting the alert saying - Changed! ');
Submitform();//Function for submitting form
});
});
Function for submitting the form 提交表格的功能
function Submitform(){
$("#YourformId").submit();//Submits your form
}
you can achieve like 你可以实现像
$('.element').val('value').change(function(){
$("#form").submit();
});
http://jsfiddle.net/senstar2/CHjL5/5/ http://jsfiddle.net/senstar2/CHjL5/5/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.