SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Sweden
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Dynamic form question ?

    Hello!

    I do have this dynamic order form which works fine.

    How do I please make so I can have some text before the prices in the menus please?

    Code:
    <form>
    <select name="a" onchange="this.form.summa.value=parseFloat(this[this.selectedIndex].text)+parseFloat(this.form.b[this.form.b.selectedIndex].text)">
    	<option>79</option><option>79</option><option>99</option><option>109</option><option>139</option>
    </select>
    <select name="b" onchange="this.form.summa.value=parseFloat(this[this.selectedIndex].text)+parseFloat(this.form.a[this.form.a.selectedIndex].text)">
    	<option>0</option><option>80</option><option>160</option><option>300</option><option>500</option>
    </select>
    <input type="text" name="summa" value="">
    </form>

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    the easiest method (while keeping your code intact) would be to put the option's text within the value

    Code:
    <select name="a" onchange="this.form.summa.value=parseFloat(this[this.selectedIndex].value)+parseFloat(this.form.b[this.form.b.selectedIndex].value)"> <option value='79'>Some text</option>

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Sweden
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    I did try that now, but that always gives the total price "NaN". Thanks anyway.

    all code now:
    Code:
    <form>
    
    <select name="a" onchange="this.form.summa.value=parseFloat(this[this.selectedIndex].value)+parseFloat(this.form.b[this.form.b.selectedIndex].value)">
    
    <option value='79'>Some text</option> 
    
    <option>99</option><option>109</option><option>139</option> </select>
    
     <select name="b" onchange="this.form.summa.value=parseFloat(this[this.selectedIndex].text)+parseFloat(this.form.a[this.form.a.selectedIndex].text)"> <option>0</option><option>80</option><option>160</option><option>300</option><option>500</option>
    </select> <input type="text" name="summa" value="">
    </form>

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You getting 'NAN' because you didn't change all of the options. Also, you will be unable to get the 1st calculation without selecting and reselecting because of the onchange handler.

    Why aren't you using a function? Something like:

    Code:
    function doit(formObj, selA, selB)
    {
    formObj.summa.value = parseFloat(selA.options[selA.selectedIndex].value)
    + parseFloat(selB.options[selB.selectedIndex].value)
    }
    The example code below demonstrates the different ways you can call the above function. Note: if you cut & paste this, do not use all of the methods.

    Code:
    <form>
    <select name="a" onchange="doit(this.form,this.form.a,this.form.b)">
    <option value='0'>a</option>
    <option value='80'>b</option>
    </select>
    <select name="b" onchange="doit(this.form,this.form.a,this.form.b)">
    <option value='10'>c</option>
    <option value='180'>d</option>
    </select>
    <input type="text" name="summa" onfocus="doit(this.form,this.form.a,this.form.b)">
    <input type="button" value='do it' onclick="doit(this.form,this.form.a,this.form.b)">
    </form>
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Member
    Join Date
    Aug 2003
    Location
    Sweden
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for the help.

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no prob
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •