我有以下HTML:
<input name="20152" class="valid-decimal" id="20152" type="text" maxlength="4000" placeholder="" value="" data-fieldname="Height">
<input name="20153" class="valid-decimal" id="20152" type="text" maxlength="4000" placeholder="" value="" data-fieldname="Weight">
<input name="20155" id="testcompute" type="text" maxlength="4000" data-fieldname="BMI" placeholder="Result">
<button type="button" id="btnCalculate" onclick="FillAndSubmit('@control.OptionList')">Calculate</button>
按钮点击时调用的Javascript函数是:
function FillAndSubmit(formula) {
formulaArray = formula.split(" "); //if the passed formula is 10 * ( Height + Weight ), formulaArray items are- 10,(,Height,+,Weight,)
for(var item in formulaArray)
{
//need to iterate through each item;re-construct the formula so //that we can calculate
}
//calculate the value and set in the data-fieldname="BMI" textbox
$("input[data-fieldname='BMI']").val(CF); //CF is the constructed formula in //the above loop
基本上,
有三种不同类型的项目:
任何帮助都会得到很高的评价。 提前谢谢。
答案 0 :(得分:0)
你会做同样的事情。
var operatorList = [];
operatorList.push(["times","*",timesit]);
operatorList.push(["add","+",addit]);
function timesit(width, height){return (width * height);}
function addit(width, height){return (width + height);}
var opString = "*";
var intWidth = 20;
var intHeight = 30;
var result;
for(var i in operatorList){
if(opString === operatorList[i][1]){
result = operatorList[i][2](intWidth, intHeight);
console.log(result);
}
}
你在这里做的是向数组添加函数,然后通过将opString与数组中的第二项进行比较来收集和执行正确的函数(注意数组从0开始计数)。