When I say that I have honestly tried for a couple hours but I can't seem to get anywhere with it...
I am writing a little selector script to allow a user to select multiple options and then create a url dependent on the options the user has selected.
(I can go ahead and change out the final url myself that's not the problem)
The problem is to do with the count it automatically jumps through the script and adds the first selection twice then loads the page before you can select a second option.
I attempted to move around the if statements however this had no effect.
Could someone point me in the right direction
I went and changed some of the script around again.
So that the if(count==1) is above if(count==0).
What I now get is the url followed by:
The data from the first selection is not brought into the third if statement and then appended to the url.
Let's step through your code ... line by line. You can easily do that with pen & paper.
Please try to reproduce the steps
1 - Click single mode button ... so mode will be 'single'
2 - Oh, you initialize count with a string. Why? You should use this
4 - Inside generate_url() the first if statement will be true (because mode=='single'), so you go on with this part of the method
5 - You increase count by 1 ... so count == 1 now
6 - Now you add the "selection" to the url
7 - if count == 1 (this is true) you change window.location.href which results in an immediate page refresh
In 'dual' mode
4 - The first if will be false
5 - The second if will be true, because mode=='dual'
6 - count is still 0, so the first if will be true
7 - you copy "selection" to "selection_1" and increase count by 1 (so count == 1)
8 - the second if is true ... but IS STILL WRONG because you are using the assignment operator "=" instead of the comparison operator "==". This will set count to 1 and the result of the if condition is true
9 - you copy "selection" also to "selection_2" and increase count by 1 (so count == 2)
10 - The third if is also true because count==2, so you append both copies of "selection" to the url and immediately refresh the browser because you change window.location.href
So I changed a couple things and this is what I have got:
I also fixed the if(count==1) statement so it's not using an assignment.
I moved the if(count==1) statement above the if(count==0) statement so it would only activate the second time around.
Previously it would add the first user input twice into the url.
I am trying to get it so on the first click of one button it adds the first value dependent on what button the user clicked and on the second click it then adds a second user input and creates a url.
I now have the problem where it creates the following url
It should give the following
mywebsite.com/whatever?id=(FIRST INPUT)(SECOND INPUT)
I think it's to do with the variables not being passed from one loop to the next.
With the "var" keyword you are declaring "selection_1" and "selection_2" as local variables to the method. Those local variables will be reinitialized with each call of the method.
If you omit the "var" keyword it should work better.
|All times are GMT +1. The time now is 11:34 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.