【发布时间】:2011-03-24 20:21:05
【问题描述】:
我有一个插件应该在“选择器”的值更新时触发。在正常的 UI 交互过程中,它就像一个冠军。但是,如果“选择器”通过 JavaScript 或 jQuery 更新,它不会触发。
- 直接通过文本框更新...有效。
- 按下按钮...失败。
- 使用 jQuery 调用对 selected.val(xxx) 进行更新...失败。
插件的总体思路是自动对网格和面板等内容进行四舍五入。
任何帮助都会很棒...我整天都在努力解决这个问题!
给定以下 HTML:
<input id="myDecimalTotal" type="text" value="0.00" class="rounder-decimal" />
<input id="btnDecimalTotalTest" type="button" value="Run Total" />
使用以下选择器和 JavaScript 进行测试:
jQuery(document).ready(function() {
jQuery('input.rounder-decimal').numericRounder();
jQuery('#btnDecimalTotalTest').click(overwriteDecimalTotal); // fails
jQuery('#myDecimalTotal').val(777); // fails
});
function overwriteDecimalTotal() {
jQuery('#myDecimalTotal').val(123);
}
对于以下插件:
(function($) {
$.fn.numericRounder = function(options) {
switch (typeof (options)) {
case 'object':
options = $.extend({}, $.fn.numericRounder.defaults, options);
break;
case 'string':
options = $.extend({}, $.fn.numericRounder.defaults, { onEvent: options });
break;
default:
options = $.fn.numericRounder.defaults;
}
return this.each(function() {
var element = $(this);
if (element.is('input.rounder-decimal')) {
switch (options.onEvent) {
case 'change':
element.change(roundDecimal);
break;
case 'blur':
element.blur(roundDecimal);
break;
case 'click':
element.click(roundDecimal);
break;
default:
element.blur(roundDecimal);
}
}
if (element.is('input.rounder-wholeNumber')) {
switch (options.onEvent) {
case 'change':
element.change(function() { roundWholeNumber(this, options.factorOf); });
break;
case 'blur':
element.blur(function() { roundWholeNumber(this, options.factorOf); });
break;
case 'click':
element.click(function() { roundWholeNumber(this, options.factorOf); });
break;
default:
element.blur(function() { roundWholeNumber(this, options.factorOf); });
}
}
/// <summary>Rounds a numeric value to the nearest place.</summary>
function roundDecimal() {
var value = $(this).val();
value = extractValue(value);
if (isNaN(value))
value = $(this).val();
else
value = Math.round(value).toFixed(2);
$(this).val(value);
}
/// <summary>Rounds a numeric value to the nearest place.</summary>
function roundWholeNumber(element, factorOf) {
var value = $(element).val();
value = extractValue(value);
if (isNaN(value))
value = $(element).val();
else
value = Math.round(value / factorOf) * factorOf;
$(element).val(value);
}
/// <summary>Extracts the number.</summary>
function extractValue(value) {
var numericRegEx = /([\d\.])/g;
try {
return value.match(numericRegEx).join('');
}
catch (error) {
return value;
}
}
});
};
/// <summary>Default options.</summary>
$.fn.numericRounder.defaults = { onEvent: 'change', factorOf: 10 };
})(jQuery);
【问题讨论】:
标签: javascript jquery jquery-plugins jquery-events