2017-07-01 5 views
0

Ich möchte den folgenden Code minimieren und unter Beibehaltung der Funktionalität. bitte schlagen Sie mir Muster vor, die ich für diese Art von Problemen verwenden kann.Wie Sie diese Art von Js-Code kurz machen

// tb bid pickup location picker. 
$('#--tb-bid-pickup-location').on('keyup', function() { 
    toggler.toggler('#--tb-bid-location-picker', 'pulse'); 
}); 

// tb bid drop location picker. 
$('#--tb-bid-destination-location').on('keyup', function() { 
    toggler.toggler('#--tb-bid-location-picker', 'pulse'); 
}); 

// tb bid drop location picker. 
$('#--tb-bid-vehicle-type').on('keyup', function() { 
    toggler.toggler('#--tb-bid-truck-picker', 'pulse'); 
}); 
+1

mehrere Selektoren (getrennt durch ',') und 'this' im Ereignishandler –

+0

Sie Ihre js minify finden Sie [hier] (https://javascript-minifier.com/) –

+0

nein, ich will es überschaubarer zu machen, ist nicht das, was ich brauche. –

Antwort

0

Wie wäre es mit einem "Equalalency-Objekt"?

Ein 2-spaltiges Blatt mit Strings, auf das von "clocked" zu "affected" verwiesen wird.

var equiv = { 
    "--tb-bid-pickup-location"  : "--tb-bid-location-picker", 
    "--tb-bid-destination-location" : "--tb-bid-location-picker", 
    "--tb-bid-vehicle-type"   : "--tb-bid-truck-picker" 
}; 

$("[id^='--tb-bid']").on('keyup', function() { 

    // Get the clicked ID. 
    var thisID = $(this).attr("id"); 

    // If that id exist in the equiv object. 
    if(equiv.hasOwnProperty(thisID)){ 
    toggler.toggler("#"+equiv[thisID], 'pulse'); 
    } 
});