[英]How to show alert when / if input value live change using jquery?
I am trying to show alert if input value change. 如果输入值更改,我试图显示警报。 I am using jQuery, so here is a demo what i am trying to do but alert not showing / not working. 我正在使用jQuery,因此这是我正在尝试做的演示,但警告未显示/不起作用。 Thanks 谢谢
$(".d1, .d2").click( function (e) { var $this = $(this); $(".input").attr('value', $this.text()); }); $(".input").change( function() { alert(this.value); });
.d1, .d2 {cursor:pointer;border:1px solid black;margin-top:10px;padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <input class="input" value="" /><p/> <a class="d1">Demo 1</a> <a class="d2">Demo 2</a>
You need to use .val()
method to set value of input
element, Since you are using the change
event in input
on programatically changing the value it will not execute. 您需要使用.val()
方法来设置input
元素的值,因为在input
以编程方式更改值时将在input
中使用change
事件,因此该事件不会执行。 You need to use .trigger(event)
您需要使用.trigger(event)
Execute all handlers and behaviors attached to the matched elements for the given event type. 执行给定事件类型的所有处理程序和附加到匹配元素的行为。
$(".d1, .d2").click( function (e) { var $this = $(this); $(".input").val($this.text()).trigger('change'); }); $(".input").change( function() { alert(this.value); });
.d1, .d2 {cursor:pointer;border:1px solid black;margin-top:10px;padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <input class="input" value="" /><p/> <a class="d1">Demo 1</a> <a class="d2">Demo 2</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.