javascript - How to show form fields on keyup -
I am working on it for weeks and I can not get hang on it. I'm trying to show hidden fields only when the previous fields are entered Here's my example code:
HTML
& lt; Form & gt; & Lt; Div id = "group1" & gt; & Lt; Label & gt; Field 1: & lt; / Labels & gt; & Lt; Input type = "text" class = "field 1" /> & Lt; Br / & gt; & Lt; Label & gt; Field 2: & lt; / Labels & gt; & Lt; Input type = "text" class = "field2" /> & Lt; Br / & gt; & Lt; Label & gt; Field 3: & lt; / Labels & gt; & Lt; Input type = "text" class = "field3" /> & Lt; Br / & gt; & Lt; / Div & gt; & Lt; Div id = "group2" & gt; & Lt; Label & gt; Field 4: & lt; / Labels & gt; & Lt; Input type = "text" class = "field 4" /> & Lt; Br / & gt; & Lt; Label & gt; Field 5: & lt; / Labels & gt; & Lt; Input type = "text" class = "field5" /> & Lt; Br / & gt; & Lt; Label & gt; Field 6: & lt; / Labels & gt; & Lt; Input type = "text" class = "field6" /> & Lt; Br / & gt; & Lt; / Div & gt; & Lt; Div id = "group3" & gt; & Lt; Label & gt; Field 7: & lt; / Labels & gt; & Lt; Input type = "text" class = "field7" / & gt; & Lt; Br / & gt; & Lt; Label & gt; Field 8: & lt; / Label & gt; & Lt; Input type = "text" class = "field8" /> & Lt; Br / & gt; & Lt; Label & gt; Field 9: & lt; / Label & gt; & Lt; Input type = "text" class = "field9" /> & Lt; Br / & gt; & Lt; Input type = "submit" value = "submit" & gt; & Lt; / Div & gt; & Lt; / Form & gt; css
# group 2 {visibility: hidden; } # Group 3 {visibility: hidden; } script
$ (document) .ready (function () {$ ('# group1'). Search ('input [type ='] Text "] ') .keyp (function () {checksmitt ();}); function checksubmit () {var x = true; $ (' # group1 '). (' Input [type = 'text'] '). Key (function () {if ($ (this) .val (). Length === 0} {x = false; return;}}); if (x) {$ ('# group2') CSS ('visibility ',' Visible '); $ (' # group3 '). CSS (' visibility ',' visible ');} and {$ (' # group2 '). CSS (' visibility ',' hidden '); $ ('# Group3'). CSS ('Visibility', 'Hidden');} CheckSmit ();}); I'm not sure what I'm doing wrong here, can someone help you?
I have changed your code a bit, I have stored the relevant selectors in the variable, so every time you There is no need to query again to make some changes.
Here is the update code:
Javascript
var inputs = $ ('# group1'). ('Input [type = "text"]'); Hide hidden = $ ('# group2, # group3'); Inputs.keyup (function () {var test = true; inputs.each (function (key, value) {if (! $ (This) .val (). Length} {test = false; return false;}}); Hidden.css ('visibility', (test 'view': 'hidden'));}); Demo
Comments
Post a Comment