I have many select boxes in an HTML page and I have used the same class name 'paperGSM' for all select elements and a function which triggers on change of the select box elements value.
var getPaperTypeFromGSM = function()
{
$('div').off().on('change', '.paperGSM', function ()
{
console.log($(this).val());
var url = 'api/getPaperTypeFromGSM';
var postData = {
paperGSM: $(this).val()
};
var $current = $(this);
$.post(url, postData, function (result)
{
if (result.result == 1) {
var output = "";
output += '';
for (var i = 0; i ' + result.paperType[i].paper_type_name + '';
}
$current.siblings('.paperType').prop("disabled", false).html(output);
getPaperSizeFromPaperType();
} else {
Result.error(result);
}
}, 'json');
});
return false;
};
Every time the change event occurs it triggers multiple times (maybe it triggers for all the elements with the same class name). The image below explains the situation.
每次发生更改事件时,它都会多次触发(可能会触发具有相同类名的所有元素)。下图说明了这种情况。
P.S: My logic and even the post function are working fine
P.S:我的逻辑甚至后期功能都运行良好
As I do not want to hardcode the events for all elements (like give all elements different class name and hardcode the functions for all), I only want one function working for all the same class elements but trigger it only once.