javascript - Chained html number selects -


Due to being very new to JavaScript, I have two HTML numbers in which I select from years and I want to make the second selection with the first one in a way when I select one year in the first selection (while the second is not the first option), the second selection should be included from the selected year to the current year.

My sample HTML code:

  & lt; Select ID = "Select 1" onchange = "gener_selectNumbers ()" & gt; & Lt; Option value = "select" selected & gt; Select one year & lt; / Option & gt; & Lt; Option value = "2000" & gt; 2000 & lt; / Options & gt; & Lt; Option value = "2001" & gt; 2001 & lt; / Options & gt; & Lt; Option value = "2002" & gt; 2002 & lt; / Options & gt; & Lt; / Select & lt; Select ID = "Select 2" & gt; & Lt; / Select & gt;  

My JavaScript code:

  & lt; Script & gt; Function gener_selectNumbers () {var a = document.getElementById ("select 1"); Var b = document.getElementById ("second selection"); Var i = a.options [a.selectedIndex] .value; Var d = new date (); Var n = d.getFullYear (); {B.options [b.options.length] = New option ('I', 'I') for (I, i & lt; = n, i ++); }} & Lt; / Script & gt;  

Every time I select 1 year from the selection, the other is not the same and remains empty if I am completely wrong then I can improve my JS code or new I appreciate using code or should I use AJAX for other selection to auto refresh with new options? thank you in advanced.

I have set a bit of code and it starts working. Working here. HTML file I recommend you use more verbose variable names.

This is

Edit: Updated to choose the second option before inserting the new one.

  & lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" & gt; Function gener_selectNumbers () {var a = document.getElementById ("Selen"); Var b = document.getElementById ("selTwo"); // clear option for the second option (b) in option B) {b.remove (option); } Var i = a.options [a.selectedIndex] .value; Var d = new date (); Var n = d.getFullYear (); {B.options [b.options.length] = new option (i, i) for (I; i & lt; = n; i ++); }} & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Select id = "selOne" onchange = "gener_selectNumbers ()" & gt; & Lt; Option value = "select" selected & gt; Select one year & lt; / Option & gt; & Lt; Option value = "2000" & gt; 2000 & lt; / Options & gt; & Lt; Option value = "2001" & gt; 2001 & lt; / Options & gt; & Lt; Option value = "2002" & gt; 2002 & lt; / Options & gt; & Lt; / Select & gt; & Lt; Select id = "selTwo" & gt; & Lt; / Select & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -