SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with interdependent form fields

    I have four form fields that should be autocompleted based on the input of the user, in the following way: when the first field is filled in, the second field should be filled with the first one's value * 2. When the 3rd field is filled, the fourth one should be filled with this third value * the first one.

    I have no problems with the first two fields, but the third and fourth one keep returning 0 as a result.

    Code:
    $(function() {
    	
    	var $val1 = 0;
    	var $val2 = 0;
    	
    	$('input[name="first"]').keyup(function() {
    		$val1=parseInt($(this).val());
    		$('input[name="second"]').val($val1*2);
    	}),
    	
    	$('input[name="third"]').keyup(function() {
    		$val2 = parseInt($(this).val());
    		$('input[name="fourth"]').val($val2*$val1);
    	})
    })

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Your code works for me, apart from seeing NaN when you first click in an empty field.

    You may have to provide a sample test page that allows us to experience the same issue that you're facing.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, here you go: Area Linea Domus - Login

    Credentials are: test test

    Go to the bottom of the page, there is only one row, click on Edit. Now if I insert, let's say, 2 into Coppie, Persone becomes 4, but if I write 20.00 into Contributo a coppia, Contributo serata remains 0 (it should be 20.00 * 2).

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The script seems to be in file called calcoloautomatico.js

    Could the reason be due to the keyup code for that second field being commented out?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The script seems to be in file called calcoloautomatico.js

    Could the reason be due to the keyup code for that second field being commented out?
    Sorry, I forgot to update the code in the online version. The content of calcoloautomatico.js is the one I've posted in my first post in this topic. Even if nothing is commented out, I get the same problem.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    When trying to edit, there is now the message:
    "Non possibile modificare un incontro di cui non si il relatore."
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should work now

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by D3V4 View Post
    Go to the bottom of the page, there is only one row, click on Edit. Now if I insert, let's say, 2 into Coppie, Persone becomes 4, but if I write 20.00 into Contributo a coppia, Contributo serata remains 0 (it should be 20.00 * 2).
    That seems to work now.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you are lucky, because it doesn't work for me lol

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by D3V4 View Post
    Well, you are lucky, because it doesn't work for me lol
    Okay, I'll now move from trying in Chrome to trying in IE.

    yes, it works for me in IE too. Perhaps your web browser is still caching the old version?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was a cache problem Thanks!


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
  •