English 中文(简体)
有条件声明
原标题:Jquery conditional statement

I m really sorry if this come across as stupid but i ve searched everywhere but i didn t find any tip on how to go about it. i have this variable

 var id = $( #sometextfield ).val(); 

这一案文的外地价值是动态产生的。

 <input type="text" name="id" id="id" value="<?php echo $_get[something];?>" />)

这里的问题是,如果情况是这样的话。

if(id == 100 || id ==120) {
   // i have these variables
   var mulitiplier = 0.005
   var price = (some alog) * mulitiplier;

   // do some very long piece of code
}
else if (id == 200 || id == 220) {
   // Then i have these variables
   var mulitiplier = 0.090;
   var price = (some alog) * mulitiplier;
   // Do the same very long piece of code(its practically the same thing as 
   // the first if statement and the only change is the ariable multiplier)        
}

This works and all but is there any way to not repeat the same thing. I don t like the look of it atm. Many thanks in advance..

最佳回答
function long_piece(multiplier){
    ... long piece of code....
}
var MultMap = {100:0.005, 120:0.005, 200:0.009, 220:0.009}

long_piece(MultMap[id])
问题回答

简略地列出将“条码”和“条码”数值作为参数的功能。 例如

var theCode = function (multiplier, price) {
  do some very long piece of code
};

if(id == 100 || id == 120) {
  var mulitiplier = 0.005
  var price = (some alog) * mulitiplier;
  theCode(multiplier, price);
} else if (id == 200 || id == 220) {
  var mulitiplier = 0.090;
  var price = (some alog) * mulitiplier;
  theCode(multpilier, price);
}

注:您应考虑在《<代码>>id和的初始化中使用parseInt,用于比较此类编码的数值而不是=

简便,简单地将“极长的法典”置于习惯功能中,接受1个参数,即你的乘数。

在一项职能中发掘长篇的法典,并重写你的法典,这样会更有效率(而且更可读)。

var id100120 = id === 100 || id === 120,
    id200220 = id === 200 || id === 220,
    multiplier = id100120 ? 0.005 : id200220 ? 0.090 : null,
    price = (/*some alog*/) * mulitiplier,
    longPieceOfCode = function(){ /* your code here ... */ };

if (id100120 || id200220) {
  longPieceOfCode();
}

如果id 是直观价值,你也可以使用类似的东西:

var id100120 =  [100][120] .indexOf( [ +id+ ] )>-1,
    id200220 =  [200][220] .indexOf( [ +id+ ] )>-1

或(除ie外;9 我认为)

id = Number(id);
var id100120 = [100,120].indexOf(id) > -1,
    id200220 = [200,220].indexOf(id) > -1




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.