在 jQuery 中强制一个事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1251547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force an event in jQuery
提问by Bob Knothe
To all;
给大家;
I have created a up and down counter for decimal places and when a change occurs I have it force a blur event to recalculate fields with the following code:
我为小数位创建了一个向上和向下计数器,当发生变化时,我强制一个模糊事件使用以下代码重新计算字段:
$('button').click(function(){
var decPlaces = document.calculator.dpv.value * 1;
var hii = document.calculator.origin.value;
if (this.id == 'up' && decPlaces < 9){
document.calculator.dpv.value = decPlaces + 1;
if (hii != ''){
document.calculator[hii].focus();
document.calculator[hii].blur();
}
}
if (this.id == 'down' && decPlaces > 0){
document.calculator.dpv.value = decPlaces - 1;
if (hii != ''){
document.calculator[hii].focus();
document.calculator[hii].blur();
}
}
Works well in FF but drags in others particularly IE - suggestions on making the cleaner and faster is appreciated.
在 FF 中运行良好,但在其他情况下尤其是 IE 中会拖累 - 建议您更清洁、更快。
Bob
鲍勃
回答by Julian de Wit
The official jquery way to trigger/force an event is
触发/强制事件的官方 jquery 方法是
$("selector").trigger("blur");
$("selector").trigger("focus");
But I'm not sure this is what will help you.
但我不确定这对你有帮助。
回答by CMerat
You're mixing jQuery and DOM calls, you should really avoid doing that.
您正在混合 jQuery 和 DOM 调用,您真的应该避免这样做。
Create specific handlers for the Down and Up buttons (by using either ID tags or class tags) and then change the value of your calculator value by calling the jQuery $("#calculator").val(decPlaces + 1);
为向下和向上按钮创建特定的处理程序(通过使用 ID 标签或类标签),然后通过调用 jQuery 更改计算器值的值 $("#calculator").val(decPlaces + 1);
回答by Bob Knothe
After seeing some of the helpful comments I have made the following changes:
在看到一些有用的评论后,我进行了以下更改:
jQuery(function($) {
$("button").bind("click", function(e){
var decPlaces = $('#dpv').val() * 1;
var hi1 = $('#origin').val();
if (this.id == 'up' && decPlaces < 5){
$('#dpv').val(decPlaces + 1);
if (hi1 != ''){
$('#' + hi1).trigger("blur");
}
}
if (this.id == 'down' && decPlaces > 0){
$("#dpv").val(decPlaces - 1);
if (hi1 != ''){
$('#' + hi1).trigger("blur");
}
}
});
$('input.auto').focus(function(){
if (this.id != 'dpv'){
$(this).parent().addClass("curFocus")
}
});
$('.clearAll').focus(function(){
$('.clearAll').val("");
});
$('input.auto').blur(function(){
$(this).parent().removeClass("curFocus")
var sqft = 10.76391041670972192890; //square feet per square meter
var lbs = 2.20462262184877566540; //pounds per kilo
var bwiv = '';
var sfiv = '';
var bwmv = '';
var smmv = '';
$('#origin').val(this.id);
if((this.id == 'bwi' || this.id == 'sfi') && this.value != ''){ // imperial
if(this.id == 'bwi'){
bwiv = $.fn.autoNumeric.Strip(this.id);
sfiv = (3000 / bwiv);
$('#sfi').val($.fn.autoNumeric.Format('sfi', sfiv));
}
if(this.id == 'sfi'){
sfiv = $.fn.autoNumeric.Strip(this.id);
bwiv = (3000 / sfiv);
$('#bwi').val($.fn.autoNumeric.Format('bwi', bwiv));
}
bwmv = (((bwiv / lbs) / (3000 / sqft)) * 1000);
smmv = (1000 / bwmv);
$('#bwm').val($.fn.autoNumeric.Format('bwm', bwmv));
$('#smm').val($.fn.autoNumeric.Format('smm', smmv));
}
if((this.id == 'bwm' || this.id == 'smm') && this.value != ''){ //metric
if(this.id == 'bwm'){
bwmv = $.fn.autoNumeric.Strip(this.id);
smmv = (1000 / bwmv);
$('#smm').val($.fn.autoNumeric.Format('smm', smmv));
}
if(this.id == 'smm'){
smmv = $.fn.autoNumeric.Strip(this.id);
bwmv = (1000 / smmv);
$('#bwm').val($.fn.autoNumeric.Format('bwm', bwmv));
}
bwiv = ((((bwmv / 1000) * lbs) / sqft) * 3000);
sfiv = (3000 / bwiv);
$('#bwi').val($.fn.autoNumeric.Format('bwi', bwiv));
$('#sfi').val($.fn.autoNumeric.Format('sfi', sfiv));
}
});
});
The up down buttons that increase or decrease the decimal setting still are not very responsive in IE.
在 IE 中增加或减少小数设置的向上向下按钮仍然不是很灵敏。
FYI - the autoNumeric function call is to a plugin that I created that that does numeric formatting on the fly.
仅供参考 - autoNumeric 函数调用是我创建的一个插件,该插件可以动态进行数字格式设置。
Thanks again.
再次感谢。
Bob
鲍勃
回答by Che Carsner
I hate unreadable code, so I just formatted that for you :)
我讨厌不可读的代码,所以我只是为你格式化了:)
jQuery(function($) { $("button").bind("click", function(e){
var decPlaces = $('#dpv').val() * 1;
var hi1 = $('#origin').val();
if (this.id == 'up' && decPlaces < 5){
$('#dpv').val(decPlaces + 1);
if (hi1 != ''){
$('#' + hi1).trigger("blur");
}
}
if (this.id == 'down' && decPlaces > 0){
$("#dpv").val(decPlaces - 1);
if (hi1 != ''){
$('#' + hi1).trigger("blur");
}
}
});
$('input.auto').focus(function(){
if (this.id != 'dpv'){
$(this).parent().addClass("curFocus")
}
});
$('.clearAll').focus(function(){ $('.clearAll').val(""); });
$('input.auto').blur(function(){
$(this).parent().removeClass("curFocus")
var sqft = 10.76391041670972192890; //square feet per square meter
var lbs = 2.20462262184877566540; //pounds per kilo
var bwiv = '';
var sfiv = '';
var bwmv = '';
var smmv = '';
$('#origin').val(this.id);
if((this.id == 'bwi' || this.id == 'sfi') && this.value != ''){
// imperial
if(this.id == 'bwi'){
bwiv = $.fn.autoNumeric.Strip(this.id);
sfiv = (3000 / bwiv);
$('#sfi').val($.fn.autoNumeric.Format('sfi', sfiv));
}
if(this.id == 'sfi'){
sfiv = $.fn.autoNumeric.Strip(this.id);
bwiv = (3000 / sfiv);
$('#bwi').val($.fn.autoNumeric.Format('bwi', bwiv));
}
bwmv = (((bwiv / lbs) / (3000 / sqft)) * 1000);
smmv = (1000 / bwmv);
$('#bwm').val($.fn.autoNumeric.Format('bwm', bwmv));
$('#smm').val($.fn.autoNumeric.Format('smm', smmv));
}
if((this.id == 'bwm' || this.id == 'smm') && this.value != ''){ //metric
if(this.id == 'bwm'){
bwmv = $.fn.autoNumeric.Strip(this.id);
smmv = (1000 / bwmv);
$('#smm').val($.fn.autoNumeric.Format('smm', smmv));
}
if(this.id == 'smm'){
smmv = $.fn.autoNumeric.Strip(this.id);
bwmv = (1000 / smmv);
$('#bwm').val($.fn.autoNumeric.Format('bwm', bwmv));
}
bwiv = ((((bwmv / 1000) * lbs) / sqft) * 3000);
sfiv = (3000 / bwiv);
$('#bwi').val($.fn.autoNumeric.Format('bwi', bwiv));
$('#sfi').val($.fn.autoNumeric.Format('sfi', sfiv));
}
});
});
});
回答by Gabriel Andrés Brancolini
The easiest way is just to trigger a change() event after you change your value!
最简单的方法是在更改值后触发 change() 事件!
For example, if you write
例如,如果你写
$('selector').text('I am changing some text').change();
that should work!
那应该工作!