简体   繁体   中英

Javascript - how to get a float value from an HTML input?

I am experiencing some problem retrieving float numbers value with JavaScript from HTML by clicking on a button and then process them into a PHP file.

The HTML part is inside a table (maybe is usefull for extra info):

<table style="width:100%">
  <tr>
   <!-- some other <td> here -->
    <td>
    <img name="clickImage" style="position: relative;  margin: auto;" src="myImage.png" onClick="fooFloat()">
    </td>
    <td>
    <input id="toAngleH" maxlength="6" size="6" type="text" value="" style="background-color: #ffff;"/>
        <br>
    <input id="toAngleV" maxlength="6" size="6" type="text"value="" style="background-color: #ffff;"/>
    </td>
  </tr>
</table>

And my Javascript function fooFloat() is the following:

function fooFloat()
{    
  //doesn't work
  var $angleH = parseFloat(document.getElementById("toAngleH").value);
  var $angleV = parseFloat(document.getElementById("toAngleV").value);

  //doesn't work
  //var $angleH = $("#toAngleH").val();
  //var $angleV = $("#toAngleV").val();

  $("#content").load("file.php",{ toAngleH: angleH, toAngleV: angleV });
}

In the PHP file the values are retrieved as always with the

isset($_POST["string"])

function which works fine for integer numbers, but seems I cannot process the float values into the Javascript function.

I don't know where I am wrong. Thank you in advance for your help.

EDIT:

I finally solved the problem. I was missing an apostrophe in the PHP file...

Bye.

Did you try passing the number as a string and parsing it on the server side? I'd imagine there is some kind of issue with the point or comma. You could maybe try replacing those characters before posting and then replacing then back on the server before type changing.

In http post request the data is serialized/encoded to string and posted to server. Now it's server responsibility to parse these data correctly.
As far as Javascript is concerned you are retrieving it correctly . However i think you have a typo where you storing the value in $angleH but using angleH ($ omitted). You may wish to check network headers through debugger network tab to see what data is posted to the server.

var val = document.getElementById("toAngleV").value//string value

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