需要有关两个输入框上的更改事件的帮助
我需要这方面的帮助....我想触发警报(),例如在两个输入框上的更改事件之后执行一些代码....这是我的代码..
Millimeter: <input type="text" id="millimeter" class="filter"/>
Inch: <input type="text" id="inch" class="filter"/>
<script type="text/javascript">
$(document).ready(function(){
$(".filter").change(function(){
var value = this.value;
var id = this.id;
var convert = "";
switch(id)
{
case "millimeter":
convert = (value / 25.4).toFixed(2); //converts the value of input(mm) to inch;
$("#inch").val(convert).change();
break;
case "inch":
convert = (value * 25.4).toFixed(2); //converts the value of input(inch) to mm;
$("#millimeter").val(convert).change();
break;
default:
alert('no input has been changed');
}
alert(id+" triggered the change() event");
//some code here....
});
});
</script>
我想要的是触发警报( ) 2 两次...结果看起来像这样...“毫米触发了change()事件”...然后当另一个输入框改变其值时...“英寸触发了change()事件“...反之亦然...我是 javascript 和 jquery 的新手...任何帮助将不胜感激..
i need help on this one....i want to trigger the alert() e.g some code to execute after the change event on both input boxes....here is my code..
Millimeter: <input type="text" id="millimeter" class="filter"/>
Inch: <input type="text" id="inch" class="filter"/>
<script type="text/javascript">
$(document).ready(function(){
$(".filter").change(function(){
var value = this.value;
var id = this.id;
var convert = "";
switch(id)
{
case "millimeter":
convert = (value / 25.4).toFixed(2); //converts the value of input(mm) to inch;
$("#inch").val(convert).change();
break;
case "inch":
convert = (value * 25.4).toFixed(2); //converts the value of input(inch) to mm;
$("#millimeter").val(convert).change();
break;
default:
alert('no input has been changed');
}
alert(id+" triggered the change() event");
//some code here....
});
});
</script>
what i want is to trigger the alert() 2 twice...the result would be look like this..."Millimeter triggered the change() event"...and then when the other input box changes its value...."Inch triggered the change() event"....vice versa...i'm new to javascript and jquery...any help would be much appreciated..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码的问题在于,在第一个文本框的更改事件中,您触发了第二个文本框的更改事件,从而进入无限循环。您应该仅使用以下:
和:
来设置其他字段的值,但不会再次触发更改。
The problem with your code is that in the change event of the first textbox you are triggering the change event of the second and thus entering in an endless loop. You should only use the following:
and:
in order to set the value of the other field but do not trigger change again.
在 jsFiddle 上运行脚本时出现“超出最大调用堆栈大小”错误。这是因为您在其内部调用
.change()
函数。我把它删除了,效果很好。小提琴:http://jsfiddle.net/EwdLs/
Running your script on jsFiddle got me a "Maximum call stack size exceeded" error. This is because you're calling your
.change()
function inside of itself. I removed it, and it works fine.Fiddle: http://jsfiddle.net/EwdLs/
如果您希望每个输入的更改也触发其他输入的更改,但又不想陷入无限循环,请尝试以下一些变体:
如果不明显,我基本上采用了现有的更改处理程序并将其放入一个新函数
applyChange
,它有一个参数来告诉它是否递归。代码本身很笨重,但它应该为您提供一种完成您似乎要求的操作的方法的总体思路。PS 请务必添加一些验证,以确保用户输入的确实是一个数字。
If you want each input's change to also trigger the other input's change, but don't want to get in an endless loop, try some variation on the following:
In case it's not obvious, I basically took your existing change handler and put it in a new function,
applyChange
, which has a parameter to tell it whether or not to recurse. The code as is is clunky, but it should give you the general idea of one way to do what you seem to be asking.P.S. Be sure to add in some validation that what the user entered is really a number.