• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Having trouble with form field math binding

Explorer ,
Mar 05, 2013 Mar 05, 2013

Copy link to clipboard

Copied

I have a page that outputs a bunch of rows with form fields from a query. I am trying to bind a totals field to some simple math between a couple of the form fields and cannot get it working for some reason. What I am trying to do is:

bind = {((field1_#id# -field2_#id#) + #field3_#id#)}

What would be the best way to go about this?

TOPICS
Advanced techniques

Views

488

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2013 Mar 09, 2013

Copy link to clipboard

Copied

LATEST

<script type="text/javascript">

function getResult(num1, num2, num3) {

    //converts a blank, space, null, NaN, etc. to 0

    var num1 = num1 || 0;

    var num2 = num2 || 0;

    var num3 = num3 || 0;

    // For decimal numbers, use parseFloat instead

    return parseInt(num1)-parseInt(num2)+parseInt(num3);

}

</script>

<!--- My example, just to simulate your context. --->

<cfset id="x">

<cfform number="mycfform">

    First Number: <cfinput type="text" name="firstnumber_#id#" required="yes"><br>

    Second Number: <cfinput type="text" name="secondnumber_#id#" required="yes"><br>

    Third Number: <cfinput type="text" name="thirdnumber_#id#" required="yes"><br>

    <!--- I have added the 'keyup' event to the last parameter to ensure the result appears immediately, on-key-up.--->

    Calculation Result: <cfinput name="result" type="text" bind="javascript:getResult({firstnumber_#id#},{secondnumber_#id#},{thirdnumber_#id#@keyup})" bindonload="true">

</cfform>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation