Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form field auto-calculations

Status
Not open for further replies.

Xunil

Technical User
Dec 17, 2002
27
0
0
GB
This is the scenario:

I want to be able to input a numerical value in a form field.

Depending on that value, the form field below it will automatically populate with another numerical value.

In Excel I do it like this:

=IF(L2>=1000001,"700",IF(L2>=500001,"420",IF(L2>=200001,"220",IF(L2>=100001,"150",IF(L2>=80001,"100",IF(L2>=50001,"60",IF(L2<=50000,"40")))))))

Basically if the input value of text box 1 is greater than 1,000,001 then the next text box will read 700

At greater than or equal to 500,001 it will read 420.

And so on...

I would also like to add a third text box that will automatically calculate a percentage based on the first text box value, so if text box 1 is greater than or equal to 500,001 then text box 3 will equal 4% of the value of text box 1. If text box 1 is greater than or = to 250,001 then text box 3 will = 3% of value of text box 1, and so on...

That's the easiest way to describe what I'm trying to achieve.

Eventually I'd like to incorporate VAT calculations and other bits and bobs, all of which will be saved into a database via ASP when a save/submit button is clicked.

All I've managed so far is a very large headache.

Any (polite) suggestions gratefully received.
 
this looks like a job for the switch statement. allow me to show you a simplified version:

Code:
function doSwitch( oSrc, oTarg ) {
    var intTargVal;
    switch( parseInt( oSrc.value, 10 ) ) {
        case >= 10000001:
            intTargVal = 700;
            break;
        case >= 5000001:
            intTargVal = 420;
            break;
        case >= 2000001:
            intTargVal = 220;
            break;
        case >= 1000001:
            intTargVal = 150;
            break;
        case >= 80001:
            intTargVal = 100;
            break;
        case >= 50001:
            intTargVal = 60;
            break;
        default:
            intTargVal = 40;
            break;
    }

    oTarg.value = intTargVal;
}

your call would be something like this:

Code:
<table><tr>
  <td><input name="t1" type="text" onblur="doSwitch(this,this.form.t2);" /></td>
  <td><input name="t2" type="text" /></td>
</tr></table>

i'm sure this may be a little confusing. let me know if you need anything clarified.



*cLFlaVA
----------------------------
[tt]( <P> <B>)[sup]13[/sup] * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
If you ask me the time about now you can watch my head explode.

:)

Serves me right for asking I guess...

OK, I'll make a pot of coffee, sit down and read through your example again and attempt to make an ounce of sense out of it.

JavaScript to me is like a spider crawling over the page after having trodden in ink *sigh*

Thanks for the reply - much appreciated, but I can see I have a long ay to go on this one.
 
this isn't confusing, you just think it is.

a switch statement is like a consolidated if-else if-else statement.

first, you need to tell JS what value to compare against...

Code:
var myVariable = 3;

switch (myVariable) {
  // ...
}

next, you will need to specify which cases you want to handle for:

Code:
case 1:
  // ...
case 2:
  // ...
case 3:
  // ...
default:
  // ...

the [tt]break[/tt] statement means basically, don't continue in this statement, go to the first line of code after the switch block.

so, let's put it together with some working code...

Code:
var myVariable = 3;
switch (myVariable) {
  case 1:
    alert('variable has a value of 1');
    break;
  case 2:
    alert('variable has a value of 2');
    break;
  case 3:
    alert('variable has a value of 3');
    break;
  default:
    alert('variable has some other value (not 1 - 3)');
    break;
}

so, what do you think will happen? try changing the value of the variable...



*cLFlaVA
----------------------------
[tt]( <P> <B>)[sup]13[/sup] * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Hey that almost makes sense (entirely my fault and certainly not a reflection on your explanation).

I'll spend some time digesting it and fooling with the variables to see if I can get it to click.

Wish me luck (I'll need it).

Thanks for the input.

Watch this space...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top