简体   繁体   中英

Ignore input if it has no value

I've made a very simple calculator, i'm sure there is a better way of writing it, using JS to calculate the amount of rolls of wallpaper required. You have wall length, wall height, roll width, roll length and pattern repeat. My calculator works apart from when the pattern repeat is blank or 0 - So i need to ignore the value of input name="number3" (e)from my calculation if it is 0 - but not quite sure how to do this. Can i do it this with how I currently have written it or do i need to set the calculation differently?

function multiplymetric(){
    var a=Number(document.metriccalculator.number1a.value);
    var b=Number(document.metriccalculator.number1b.value);
    var c=Number(document.metriccalculator.number2a.value);
    var d=Number(document.metriccalculator.number2b.value);
    var e=Number(document.metriccalculator.number3.value);
    var f=Math.ceil(a/c);
    var g=Math.ceil(b/e);
    var h=Math.floor(d/(e*g));
    var i=Math.ceil(f/h);
    document.metriccalculator.total.value=i;
}

<form name="metriccalculator"> 
<input type="text" size="10" height="15" name="number1a">
<input type="text" size="10" height="15" name="number1b">
<input type="text" size="10" height="15" name="number2a">
<input type="text" size="10" height="15" name="number2b">
<input type="text" size="10" height="15" name="number3">
<input type="button" id="calcbutton" onclick="javascript:multiplymetric();" value="Calculate number of rolls">
<input type="text" size="10" height="15" name="total">

Substitute the instruction:

var g=Math.ceil(b/e);

with:

var g=Math.ceil(b/(e===0?1:e));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM