简体   繁体   中英

How to handle input data on button press?

I have the following code which runs but not the way it should. The user has to select a value from the drop-down list then, depending on the value selected, an area will be computed using the data in the input fields. The switch works fine because I can see the alert but the area returned ( aria1 / aria2 ) is shown as NaN.

FIDDLE HERE: https://jsfiddle.net/aLgvwq3r/

Select "Forma 1" and insert some values. Instead of NaN I should be getting a value for that area after I press the button.

It looks like it is not values from the inputs. What should I do?

function arie1() {
  var lungC = parseFloat(document.getElementById('lungC').value);
  var latC = parseFloat(document.getElementById('latC').value);
  var hC = parseFloat(document.getElementById('hC').value);
  var hBaza = parseFloat(document.getElementById('hTotal').value);
  var latBaza = parseFloat(document.getElementById('latBaza').value);
  var aria1 = 2 * lungC * latC + 2 * lungC * hTotal + 2 * latBaza * (hC + hTotal) + 2 * 0.5 * lungC + 0.5 * latBaza + 5; //se vor lua 5m2 de rezerva si se va mentiona pe site sa se mai adauge o marja de eroare
  document.getElementById('mpv').innerHTML = aria1;
  document.getElementById('mp').style.display = 'block';

}

function arie2() {
  var lungC = parseFloat(document.getElementById('lungC').value);
  var latC = parseFloat(document.getElementById('latC').value);
  var hC = parseFloat(document.getElementById('hC').value);
  var hBaza = parseFloat(document.getElementById('hTotal').value);
  var latBaza = parseFloat(document.getElementById('latBaza').value);
  aria2 = parseFloat(lungC * latC + 2 * lungC * hTotal + 2 * latBaza * (hC + hTotal) + 2 * 0.5 * lungC + 0.5 * latBaza + 5); //se vor lua 5m2 de rezerva si se va mentiona pe site sa se mai adauge o marja de eroare
  document.getElementById('mpv').innerHTML = aria2;
  document.getElementById('mp').style.display = 'block';
}

function switcherForma() {
  switch (parseInt(document.getElementById('select-forma').value, 10)) {
    case 1:
      alert("Ati ales Forma 1");
      document.getElementById('f1').style.color = "red";
      document.getElementById('calculeaza').addEventListener('click', arie1);
      break;

    case 2:

      break;

    case 3:

      break;
  }
}

Heree you go ,

Let me know if this is what you wanted

Replace your arie1 function with this:

function arie1() {
    var lungC = parseFloat(document.getElementById('lungC').value);
    var latC = parseFloat(document.getElementById('latC').value);
    var hC = parseFloat(document.getElementById('hC').value);
    var hBaza = parseFloat(document.getElementById('hTotal').value);
    var latBaza = parseFloat(document.getElementById('latBaza').value);
    var aria1 = lungC + latC + hC + hBaza + latBaza;
    document.getElementById('mpv').innerHTML = aria1;
    document.getElementById('mp').style.display = 'block';

    console.log(aria1);
  }

When you calculate the result the variable hTotal is not a number, but an element. Change the calculation to this and it works for me:

2*lungC*latC + 2*lungC*hTotal.value + 2*latBaza*(hC+hTotal.value) +2*0.5*lungC + 0.5*latBaza + 5

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