如何组合重复的JavaScript

时间:2013-04-28 18:51:23

标签: javascript jquery

我确定这是一个非常基本的问题,但我刚刚开始学习javascript(过去一直使用PHP)并且不能完全解决问题。我有基于用户输入执行计算的代码,当移动jquery滑块,更改文本框值或按下按钮时结果更新 - 所有三个触发相同的代码,但我必须重复三次代码,因为它是如何被触发的 - 有没有办法把它写成一个代码块,而不是用于onchange,onclick和内置于滑块查询的单独代码块?请为下面的长代码提前道歉......

  $(document).ready(function() {

$( "#slider" ).slider({ 
  range: false,
  min: 10000,
  max: 200000,
  value: 50000,
  step: 1000,
  slide: function( event, ui ) {
    $( "#amount" ).val( "$" + numberWithCommas(ui.value) );
  },
change: function( event, ui ) {


var maxcap;

var newValue = $( "#slider" ).slider( "value" ) ;
var adult_prem=450;
var child_prem=293;
var prem;

var ad = document.getElementById("famadult");
var adulthh = ad.options[ad.selectedIndex].value;
var ch = document.getElementById("famchild");
var childhh = ch.options[ch.selectedIndex].value;


var age=new Array(); 
age[0] = document.getElementById('adult_age').value;
if (adulthh == 2) {
age[1] = document.getElementById('adult2_age').value;
q = 2;
} 
else {q = 1;}
var agerating=new Array();
var ageprem = 0; 
for (var i=0;i<q;i++)
{ 

if (age[i] >=0 && age[i] <=20) {agerate = 0.635;}
else if (age[i] > 20 && age[i] <= 24) {agerate = 1;}
else if (age[i] == 25) {agerate = 1.004;}
else if (age[i] == 26) {agerate = 1.024;}
else if (age[i] == 27) {agerate = 1.048;}
else if (age[i] == 28) {agerate = 1.087;}
else if (age[i] == 29) {agerate = 1.119;}
else if (age[i] == 30) {agerate = 1.135;}
else if (age[i] == 31) {agerate = 1.159;}
else if (age[i] == 32) {agerate = 1.183;}
else if (age[i] == 33) {agerate = 1.198;}
else if (age[i] == 34) {agerate = 1.214;}
else if (age[i] == 35) {agerate = 1.222;}
else if (age[i] == 36) {agerate = 1.230;}
else if (age[i] == 37) {agerate = 1.238;}
else if (age[i] == 38) {agerate = 1.246;}
else if (age[i] == 39) {agerate = 1.262;}
else if (age[i] == 40) {agerate = 1.278;}
else if (age[i] == 41) {agerate = 1.302;}
else if (age[i] == 42) {agerate = 1.325;}
else if (age[i] == 43) {agerate = 1.357;}
else if (age[i] == 44) {agerate = 1.397;}
else if (age[i] == 45) {agerate = 1.444;}
else if (age[i] == 46) {agerate = 1.5;}
else if (age[i] == 47) {agerate = 1.563;}
else if (age[i] == 48) {agerate = 1.635;}
else if (age[i] == 49) {agerate = 1.706;}
else if (age[i] == 50) {agerate = 1.786;}
else if (age[i] == 51) {agerate = 1.865;}
else if (age[i] == 52) {agerate = 1.952;}
else if (age[i] == 53) {agerate = 2.040;}
else if (age[i] == 54) {agerate = 2.135;}
else if (age[i] == 55) {agerate = 2.230;}
else if (age[i] == 56) {agerate = 2.333;}
else if (age[i] == 57) {agerate = 2.437;}
else if (age[i] == 58) {agerate = 2.548;}
else if (age[i] == 59) {agerate = 2.603;}
else if (age[i] == 60) {agerate = 2.714;}
else if (age[i] == 61) {agerate = 2.810;}
else if (age[i] == 62) {agerate = 2.873;}
else if (age[i] == 63) {agerate = 2.952;}
else if (age >= 64) {agerate = 3;}
ageprem  += agerate*adult_prem;
agerating[i]=agerate;
}

if (adulthh == 2) {
$('#spouse_div').show();
} 
else {
$('#spouse_div').hide();
}

prem = ageprem+(child_prem*childhh);
prem = Math.round(prem);
var fpl1=117.31;
var fpl2=40.2;
var fpl0;

var fpl3 = ((adulthh-1)*fpl2)+(childhh*fpl2);
fpl0 = fpl1+fpl3;

var newFPL = Math.round(newValue/fpl0);
var avfull = prem/0.7;
if (newFPL >= 0 && newFPL < 130) {maxcap = 0.02;} 
else if (newFPL >= 130 && newFPL < 140) {maxcap = 0.03;} 
else if (newFPL >= 140 && newFPL < 150) {maxcap = 0.035;} 
else if (newFPL >= 150 && newFPL < 160) {maxcap = 0.04;} 
else if (newFPL >= 160 && newFPL < 170) {maxcap = 0.044;} 
else if (newFPL >= 170 && newFPL < 175) {maxcap = 0.048;} 
else if (newFPL >= 175 && newFPL < 180) {maxcap = 0.052;} 
else if (newFPL >= 180 && newFPL < 190) {maxcap = 0.055;} 
else if (newFPL >= 190 && newFPL < 200) {maxcap = 0.059;} 
else if (newFPL >= 200 && newFPL < 210) {maxcap = 0.063;} 
else if (newFPL >= 210 && newFPL < 220) {maxcap = 0.066;} 
else if (newFPL >= 220 && newFPL < 225) {maxcap = 0.069;} 
else if (newFPL >= 225 && newFPL < 230) {maxcap = 0.072;} 
else if (newFPL >= 230 && newFPL < 240) {maxcap = 0.075;} 
else if (newFPL >= 240 && newFPL < 250) {maxcap = 0.078;} 
else if (newFPL >= 250 && newFPL < 260) {maxcap = 0.081;} 
else if (newFPL >= 260 && newFPL < 270) {maxcap = 0.083;} 
else if (newFPL >= 270 && newFPL < 275) {maxcap = 0.085;} 
else if (newFPL >= 275 && newFPL < 280) {maxcap = 0.088;} 
else if (newFPL >= 280 && newFPL < 290) {maxcap = 0.09;} 
else if (newFPL >= 290 && newFPL < 300) {maxcap = 0.093;} 
else if (newFPL >= 300 && newFPL < 401) {maxcap = 0.095;} 
else {maxcap = 0;}


var conscost;
var cap;
if (newFPL > 400) {cap=0;}
else {cap = prem-Math.round((newValue*maxcap)/12);}

if (newFPL < 100) {premsub = 0;}
else if (newFPL >= 100 && newFPL < 151) {premsub = 0.24;}
else if (newFPL >= 151 && newFPL < 201) {premsub = 0.17;}
else if (newFPL >= 200 && newFPL < 251) {premsub = 0.03;}
else {premsub = 0;}
conscost = Math.max((prem-cap),0);

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
var pctincome = ((conscost*12)/newValue)*100;
pctincome = pctincome.toFixed(1);

document.getElementById("govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avful));
document.getElementById("max_cap").innerHTML=Math.round((newValue*maxcap)/12);
document.getElementById("fpl_level").innerHTML=Math.round(newValue/fpl0);
document.getElementById("mo_ins_prem_sub").innerHTML=numberWithCommas(cap);
document.getElementById("mo_ins_prem").innerHTML=numberWithCommas(prem);
document.getElementById("ann_govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull*12));
document.getElementById("ann_max_cap").innerHTML=numberWithCommas(Math.round(newValue*maxcap));
document.getElementById("ann_ins_prem").innerHTML=numberWithCommas(prem*12);
document.getElementById("ann_ins_prem_sub").innerHTML=numberWithCommas(cap*12);

document.getElementById("mo_cost").innerHTML=numberWithCommas(Math.round(conscost));
    document.getElementById("ann_cost").innerHTML=numberWithCommas(Math.round(conscost*12));
document.getElementById("ann_ins_prem_sub_pct").innerHTML=Math.round((1-(cap/prem))*100);
document.getElementById("ann_ins_cap_pct").innerHTML=Math.round((cap/prem)*100);
document.getElementById("age_factor").innerHTML=agerating[0];
document.getElementById("ann_ins_cap").innerHTML=pctincome;
}
});


function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

$( "#amount" ).val( "$" + numberWithCommas($( "#slider" ).slider( "value" )) );



$('#submit_subsidy').click(function() {
  var maxcap;
$('#subsidy_results').show();
var newValue = $( "#slider" ).slider( "value" ) ;
var adult_prem=450;
var child_prem=293;
var prem;

var ad = document.getElementById("famadult");
var adulthh = ad.options[ad.selectedIndex].value;
var ch = document.getElementById("famchild");
var childhh = ch.options[ch.selectedIndex].value;

var age=new Array(); 
age[0] = document.getElementById('adult_age').value;
if (adulthh == 2) {
age[1] = document.getElementById('adult2_age').value;
q = 2;
} 
else {q = 1;}
var agerating=new Array();
var ageprem = 0; 
for (var i=0;i<q;i++)
{ 

if (age[i] >=0 && age[i] <=20) {agerate = 0.635;}
else if (age[i] > 20 && age[i] <= 24) {agerate = 1;}
else if (age[i] == 25) {agerate = 1.004;}
else if (age[i] == 26) {agerate = 1.024;}
else if (age[i] == 27) {agerate = 1.048;}
else if (age[i] == 28) {agerate = 1.087;}
else if (age[i] == 29) {agerate = 1.119;}
else if (age[i] == 30) {agerate = 1.135;}
else if (age[i] == 31) {agerate = 1.159;}
else if (age[i] == 32) {agerate = 1.183;}
else if (age[i] == 33) {agerate = 1.198;}
else if (age[i] == 34) {agerate = 1.214;}
else if (age[i] == 35) {agerate = 1.222;}
else if (age[i] == 36) {agerate = 1.230;}
else if (age[i] == 37) {agerate = 1.238;}
else if (age[i] == 38) {agerate = 1.246;}
else if (age[i] == 39) {agerate = 1.262;}
else if (age[i] == 40) {agerate = 1.278;}
else if (age[i] == 41) {agerate = 1.302;}
else if (age[i] == 42) {agerate = 1.325;}
else if (age[i] == 43) {agerate = 1.357;}
else if (age[i] == 44) {agerate = 1.397;}
else if (age[i] == 45) {agerate = 1.444;}
else if (age[i] == 46) {agerate = 1.5;}
else if (age[i] == 47) {agerate = 1.563;}
else if (age[i] == 48) {agerate = 1.635;}
else if (age[i] == 49) {agerate = 1.706;}
else if (age[i] == 50) {agerate = 1.786;}
else if (age[i] == 51) {agerate = 1.865;}
else if (age[i] == 52) {agerate = 1.952;}
else if (age[i] == 53) {agerate = 2.040;}
else if (age[i] == 54) {agerate = 2.135;}
else if (age[i] == 55) {agerate = 2.230;}
else if (age[i] == 56) {agerate = 2.333;}
else if (age[i] == 57) {agerate = 2.437;}
else if (age[i] == 58) {agerate = 2.548;}
else if (age[i] == 59) {agerate = 2.603;}
else if (age[i] == 60) {agerate = 2.714;}
else if (age[i] == 61) {agerate = 2.810;}
else if (age[i] == 62) {agerate = 2.873;}
else if (age[i] == 63) {agerate = 2.952;}
else if (age >= 64) {agerate = 3;}
ageprem  += agerate*adult_prem;
agerating[i]=agerate;
}

if (adulthh == 2) {
$('#spouse_div').show();
} 
else {
$('#spouse_div').hide();
}

prem = ageprem+(child_prem*childhh);
prem = Math.round(prem);
var fpl1=117.31;
var fpl2=40.2;
var fpl0;

var fpl3 = ((adulthh-1)*fpl2)+(childhh*fpl2);
fpl0 = fpl1+fpl3;

var newFPL = Math.round(newValue/fpl0);
var avfull = prem/0.7;
if (newFPL >= 0 && newFPL < 130) {maxcap = 0.02;} 
else if (newFPL >= 130 && newFPL < 140) {maxcap = 0.03;} 
else if (newFPL >= 140 && newFPL < 150) {maxcap = 0.035;} 
else if (newFPL >= 150 && newFPL < 160) {maxcap = 0.04;} 
else if (newFPL >= 160 && newFPL < 170) {maxcap = 0.044;} 
else if (newFPL >= 170 && newFPL < 175) {maxcap = 0.048;} 
else if (newFPL >= 175 && newFPL < 180) {maxcap = 0.052;} 
else if (newFPL >= 180 && newFPL < 190) {maxcap = 0.055;} 
else if (newFPL >= 190 && newFPL < 200) {maxcap = 0.059;} 
else if (newFPL >= 200 && newFPL < 210) {maxcap = 0.063;} 
else if (newFPL >= 210 && newFPL < 220) {maxcap = 0.066;} 
else if (newFPL >= 220 && newFPL < 225) {maxcap = 0.069;} 
else if (newFPL >= 225 && newFPL < 230) {maxcap = 0.072;} 
else if (newFPL >= 230 && newFPL < 240) {maxcap = 0.075;} 
else if (newFPL >= 240 && newFPL < 250) {maxcap = 0.078;} 
else if (newFPL >= 250 && newFPL < 260) {maxcap = 0.081;} 
else if (newFPL >= 260 && newFPL < 270) {maxcap = 0.083;} 
else if (newFPL >= 270 && newFPL < 275) {maxcap = 0.085;} 
else if (newFPL >= 275 && newFPL < 280) {maxcap = 0.088;} 
else if (newFPL >= 280 && newFPL < 290) {maxcap = 0.09;} 
else if (newFPL >= 290 && newFPL < 300) {maxcap = 0.093;} 
else if (newFPL >= 300 && newFPL < 401) {maxcap = 0.095;} 
else {maxcap = 0;}



var conscost;
var cap;
if (newFPL > 400) {cap=0;}
else {cap = prem-Math.round((newValue*maxcap)/12);}

if (newFPL < 100) {premsub = 0;}
else if (newFPL >= 100 && newFPL < 151) {premsub = 0.24;}
else if (newFPL >= 151 && newFPL < 201) {premsub = 0.17;}
else if (newFPL >= 200 && newFPL < 251) {premsub = 0.03;}
else {premsub = 0;}
conscost = Math.max((prem-cap),0);
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
var pctincome = ((conscost*12)/newValue)*100;
pctincome = pctincome.toFixed(1);

document.getElementById("govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull));
document.getElementById("max_cap").innerHTML=Math.round((newValue*maxcap)/12);
document.getElementById("fpl_level").innerHTML=Math.round(newValue/fpl0);
document.getElementById("mo_ins_prem_sub").innerHTML=numberWithCommas(cap);
document.getElementById("mo_ins_prem").innerHTML=numberWithCommas(prem);
document.getElementById("ann_govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull*12));
document.getElementById("ann_max_cap").innerHTML=numberWithCommas(Math.round(newValue*maxcap));
document.getElementById("ann_ins_prem").innerHTML=numberWithCommas(prem*12);
document.getElementById("ann_ins_prem_sub").innerHTML=numberWithCommas(cap*12);
document.getElementById("mo_cost").innerHTML=numberWithCommas(Math.round(conscost));
document.getElementById("ann_cost").innerHTML=numberWithCommas(Math.round(conscost*12));
document.getElementById("ann_ins_prem_sub_pct").innerHTML=Math.round((1-(cap/prem))*100);
document.getElementById("ann_ins_cap_pct").innerHTML=Math.round((cap/prem)*100);
document.getElementById("age_factor").innerHTML=agerating[0];
document.getElementById("ann_ins_cap").innerHTML=pctincome;
});


$('.sub_change').change(function() {
  var maxcap;

var newValue = $( "#slider" ).slider( "value" ) ;
var adult_prem=450;
var child_prem=293;
var prem;

var ad = document.getElementById("famadult");
var adulthh = ad.options[ad.selectedIndex].value;
var ch = document.getElementById("famchild");
var childhh = ch.options[ch.selectedIndex].value;

var age=new Array(); 
age[0] = document.getElementById('adult_age').value;
if (adulthh == 2) {
age[1] = document.getElementById('adult2_age').value;
q = 2;
} 
else {q = 1;}
var agerating=new Array();
var ageprem = 0; 
for (var i=0;i<q;i++)
{ 

if (age[i] >=0 && age[i] <=20) {agerate = 0.635;}
else if (age[i] > 20 && age[i] <= 24) {agerate = 1;}
else if (age[i] == 25) {agerate = 1.004;}
else if (age[i] == 26) {agerate = 1.024;}
else if (age[i] == 27) {agerate = 1.048;}
else if (age[i] == 28) {agerate = 1.087;}
else if (age[i] == 29) {agerate = 1.119;}
else if (age[i] == 30) {agerate = 1.135;}
else if (age[i] == 31) {agerate = 1.159;}
else if (age[i] == 32) {agerate = 1.183;}
else if (age[i] == 33) {agerate = 1.198;}
else if (age[i] == 34) {agerate = 1.214;}
else if (age[i] == 35) {agerate = 1.222;}
else if (age[i] == 36) {agerate = 1.230;}
else if (age[i] == 37) {agerate = 1.238;}
else if (age[i] == 38) {agerate = 1.246;}
else if (age[i] == 39) {agerate = 1.262;}
else if (age[i] == 40) {agerate = 1.278;}
else if (age[i] == 41) {agerate = 1.302;}
else if (age[i] == 42) {agerate = 1.325;}
else if (age[i] == 43) {agerate = 1.357;}
else if (age[i] == 44) {agerate = 1.397;}
else if (age[i] == 45) {agerate = 1.444;}
else if (age[i] == 46) {agerate = 1.5;}
else if (age[i] == 47) {agerate = 1.563;}
else if (age[i] == 48) {agerate = 1.635;}
else if (age[i] == 49) {agerate = 1.706;}
else if (age[i] == 50) {agerate = 1.786;}
else if (age[i] == 51) {agerate = 1.865;}
else if (age[i] == 52) {agerate = 1.952;}
else if (age[i] == 53) {agerate = 2.040;}
else if (age[i] == 54) {agerate = 2.135;}
else if (age[i] == 55) {agerate = 2.230;}
else if (age[i] == 56) {agerate = 2.333;}
else if (age[i] == 57) {agerate = 2.437;}
else if (age[i] == 58) {agerate = 2.548;}
else if (age[i] == 59) {agerate = 2.603;}
else if (age[i] == 60) {agerate = 2.714;}
else if (age[i] == 61) {agerate = 2.810;}
else if (age[i] == 62) {agerate = 2.873;}
else if (age[i] == 63) {agerate = 2.952;}
else if (age >= 64) {agerate = 3;}
ageprem  += agerate*adult_prem;
agerating[i]=agerate;
}

if (adulthh == 2) {
$('#spouse_div').show();
} 
else {
$('#spouse_div').hide();
}

prem = ageprem+(child_prem*childhh);
prem = Math.round(prem);
var fpl1=117.31;
var fpl2=40.2;
var fpl0;

var fpl3 = ((adulthh-1)*fpl2)+(childhh*fpl2);
fpl0 = fpl1+fpl3;

var newFPL = Math.round(newValue/fpl0);
var avfull = prem/0.7;
if (newFPL >= 0 && newFPL < 130) {maxcap = 0.02;} 
else if (newFPL >= 130 && newFPL < 140) {maxcap = 0.03;} 
else if (newFPL >= 140 && newFPL < 150) {maxcap = 0.035;} 
else if (newFPL >= 150 && newFPL < 160) {maxcap = 0.04;} 
else if (newFPL >= 160 && newFPL < 170) {maxcap = 0.044;} 
else if (newFPL >= 170 && newFPL < 175) {maxcap = 0.048;} 
else if (newFPL >= 175 && newFPL < 180) {maxcap = 0.052;} 
else if (newFPL >= 180 && newFPL < 190) {maxcap = 0.055;} 
else if (newFPL >= 190 && newFPL < 200) {maxcap = 0.059;} 
else if (newFPL >= 200 && newFPL < 210) {maxcap = 0.063;} 
else if (newFPL >= 210 && newFPL < 220) {maxcap = 0.066;} 
else if (newFPL >= 220 && newFPL < 225) {maxcap = 0.069;} 
else if (newFPL >= 225 && newFPL < 230) {maxcap = 0.072;} 
else if (newFPL >= 230 && newFPL < 240) {maxcap = 0.075;} 
else if (newFPL >= 240 && newFPL < 250) {maxcap = 0.078;} 
else if (newFPL >= 250 && newFPL < 260) {maxcap = 0.081;} 
else if (newFPL >= 260 && newFPL < 270) {maxcap = 0.083;} 
else if (newFPL >= 270 && newFPL < 275) {maxcap = 0.085;} 
else if (newFPL >= 275 && newFPL < 280) {maxcap = 0.088;} 
else if (newFPL >= 280 && newFPL < 290) {maxcap = 0.09;} 
else if (newFPL >= 290 && newFPL < 300) {maxcap = 0.093;} 
else if (newFPL >= 300 && newFPL < 401) {maxcap = 0.095;} 
else {maxcap = 0;}


var conscost;
var cap;
if (newFPL > 400) {cap=0;}
else {cap = prem-Math.round((newValue*maxcap)/12);}

if (newFPL < 100) {premsub = 0;}
else if (newFPL >= 100 && newFPL < 151) {premsub = 0.24;}
else if (newFPL >= 151 && newFPL < 201) {premsub = 0.17;}
else if (newFPL >= 200 && newFPL < 251) {premsub = 0.03;}
else {premsub = 0;}
conscost = Math.max((prem-cap),0);
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
var pctincome = ((conscost*12)/newValue)*100;
pctincome = pctincome.toFixed(1);

document.getElementById("govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull));
document.getElementById("max_cap").innerHTML=Math.round((newValue*maxcap)/12);
document.getElementById("fpl_level").innerHTML=Math.round(newValue/fpl0);
document.getElementById("mo_ins_prem_sub").innerHTML=numberWithCommas(cap);
document.getElementById("mo_ins_prem").innerHTML=numberWithCommas(prem);
document.getElementById("ann_govt_subsidy").innerHTML=numberWithCommas(Math.round(premsub*avfull*12));
document.getElementById("ann_max_cap").innerHTML=numberWithCommas(Math.round(newValue*maxcap));
document.getElementById("ann_ins_prem").innerHTML=numberWithCommas(prem*12);
document.getElementById("ann_ins_prem_sub").innerHTML=numberWithCommas(cap*12);
document.getElementById("mo_cost").innerHTML=numberWithCommas(Math.round(conscost));
document.getElementById("ann_cost").innerHTML=numberWithCommas(Math.round(conscost*12));
document.getElementById("ann_ins_cap").innerHTML=pctincome;
document.getElementById("ann_ins_prem_sub_pct").innerHTML=Math.round((1-(cap/prem))*100);
document.getElementById("ann_ins_cap_pct").innerHTML=Math.round((cap/prem)*100);

document.getElementById("age_factor").innerHTML=agerating[0];

});
});

感谢您的帮助! 麦克

1 个答案:

答案 0 :(得分:0)

那么,你为什么不使用一个功能呢? 喜欢

var myApp={};
myApp.complicatedCalculation=function(parameter){/* YaddaYadda */}

所以每次都需要它,你可以打电话:

myApp.complicatedCalculation(parameter)