Simple for you : I cannot display my result

Discussion in 'Javascript' started by jd, Dec 10, 2005.

  1. jd

    jd Guest

    Try adding an onkeypress handler to quantity field.

    -- jeff
     
    jd, Dec 10, 2005
    #1
    1. Advertising

  2. jd

    RobG Guest

    wrote:
    > I have an Order form. When I change the quantity I go to a script to
    > calculate anddisplay the Amount = UnitPrice * Quantity
    >
    > But the Amount only display if I click somewhere else as the Quantity.


    That is how onchange is supposed to work - it fires when the element
    loses focus if its value has changed since gaining focus.

    <URL: http://www.merlyn.demon.co.uk >


    > How can I display it without clicking ?


    Use onkeyup, but the new value will be written to mt on every keyup, so
    make sure you account for intermediate results that may not be valid and
    don't allow errors to foul things up.


    >
    > ============================================
    > <SCRIPT LANGUAGE="JavaScript">


    The language attribute is deprecated, type is required:

    <script type="text/javascript">


    > function compute(form1) {
    > form1.mt.value=parseFloat(form1.up.value)*parseInt(form1.qt.value);


    Even though you set 'up' as readonly, a user can still change the value,
    do not assume that it is a valid float or integer. A much better idea
    is to test the value of 'up' to make sure it is OK before using it -
    similarly with the value of 'qt'.

    The values of up and qt do not need to be explicitly converted to
    numbers since they are multiplied (addition is a different matter).

    Your script could be:


    function compute(form1)
    {
    var x = form1.up.value;
    var y = form1.qt.value;
    if (validFloat(x) && validInt(y))
    form1.mt.value = x*y;
    }
    }

    Where validFloat() and validInt() are routines that check the values -
    you can find routines to do that in the archives or at:

    <URL: http://www.merlyn.demon.co.uk >


    I'd give you a better link to the number validation routines but the
    site seems to be down at the moment.


    [...]


    --
    Rob
     
    RobG, Dec 10, 2005
    #2
    1. Advertising

  3. jd

    RobG Guest

    jd wrote:
    > Try adding an onkeypress handler to quantity field.


    No, don't. That will fire before the value has been changed, so the
    last keypress will not be included in the result (unless it's a tab or
    similar).



    --
    Rob
     
    RobG, Dec 10, 2005
    #3
  4. jd

    Guest

    I have an Order form. When I change the quantity I go to a script to
    calculate anddisplay the Amount = UnitPrice * Quantity

    But the Amount only display if I click somewhere else as the Quantity.
    How can I display it without clicking ?

    ============================================
    <SCRIPT LANGUAGE="JavaScript">
    function compute(form1) {
    form1.mt.value=parseFloat(form1.up.value)*parseInt(form1.qt.value);
    }
    </SCRIPT>
    <body>
    <form name="form1" >
    <input name="up" type="text" id="up" value="15" readonly="">
    <input name="qt" type="text" id="qt" onchange="compute(this.form);">
    <input name="mt" type="text" id="mt" onfocus="this.blur()">
    </form>
    ============================================
     
    , Dec 10, 2005
    #4
  5. jd

    Guest

    It works, Thanks

    On Sat, 10 Dec 2005 11:26:19 +0100, wrote:

    >I have an Order form. When I change the quantity I go to a script to
    >calculate anddisplay the Amount = UnitPrice * Quantity
    >
    >But the Amount only display if I click somewhere else as the Quantity.
    >How can I display it without clicking ?
    >
    >============================================
    ><SCRIPT LANGUAGE="JavaScript">
    >function compute(form1) {
    >form1.mt.value=parseFloat(form1.up.value)*parseInt(form1.qt.value);
    >}
    ></SCRIPT>
    ><body>
    ><form name="form1" >
    > <input name="up" type="text" id="up" value="15" readonly="">
    > <input name="qt" type="text" id="qt" onchange="compute(this.form);">
    > <input name="mt" type="text" id="mt" onfocus="this.blur()">
    ></form>
    >============================================
     
    , Dec 10, 2005
    #5
  6. JRS: In article <439a6587$0$22277$5a62ac22@per-qv1-newsreader-
    01.iinet.net.au>, dated Sat, 10 Dec 2005 15:17:35 local, seen in
    news:comp.lang.javascript, RobG <> posted :
    >
    >Where validFloat() and validInt() are routines that check the values -
    >you can find routines to do that in the archives or at:
    >
    > <URL: http://www.merlyn.demon.co.uk >
    >
    >
    >I'd give you a better link to the number validation routines but the
    >site seems to be down at the moment.
    >


    The index page should do, but

    <URL: http://www.merlyn.demon.co.uk/js-index.htm>
    <URL: http://www.merlyn.demon.co.uk/js-valid.htm>

    are more direct.

    The site should be up AFAIK; but there is a daily bandwidth limitation
    and you may be seeing its effects.

    P.S. OTOH, reading another newsgroup, I see that the servers at Demon
    have been failing this weekend.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
    <URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
    <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
     
    Dr John Stockton, Dec 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Ram
    Replies:
    7
    Views:
    682
  2. Pavel
    Replies:
    7
    Views:
    564
    Pavel
    Sep 19, 2010
  3. Lakshmi Sreekanth

    i = 10; result = ++i - --i; How result become ZERO

    Lakshmi Sreekanth, Sep 21, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,238
    Nick Keighley
    Sep 23, 2010
  4. Mr. Buffoon
    Replies:
    4
    Views:
    355
    Eric Sosman
    Sep 23, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    1,082
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page