How to create a dynamic form using jQuery? -
I've seen a lot of help in dynamically adding rows or areas, but I'm interested in controlling those areas Which are dependent
For example, I have a form with 3 user input:
& lt; Select id = "foo" & gt; & Lt; Option value = "0" & gt; No & lt; / Options & gt; & Lt; Option value = "1" & gt; Yes & lt; / Options & gt; & Lt; / Select & gt; When the value of #foo
is set to 1
, then I would like to enable #bar
.
& lt ;! - By default, the bar should be disabled - & gt; & Lt; Select ID = "bar" & gt; & Lt; Option value = "some_1" & gt; Some 1 & lt; / Options & gt; & Lt; Option value = "some_2" & gt; Some 2 & lt; / Option & gt; & Lt; Option value = "Other" & gt; Other ... & lt; / Options & gt; & Lt; / Select & gt; When the value of #bar
is set to other
, then I would like to enable #baz
.
& lt ;! - By default, baz should be disabled - & gt; & Lt; Textarea id = "baz" & gt; & Lt; / Textarea & gt;
My goal
I want to do some guidance on writing a small plugin which provides easy access to form input with dependency.
I need brief, concise syntax. Is there any way i can add HTML attributes help to the jQuery plugin in "Automating" things?
The closest issue
- if
#foo
is set to "yes", then #bar
is set to "other" , Then #foo
is set to "no"; I have the dependency of #bar
inactive / hidden. - Input enabled / disabled and / or views / hidden; Being able to define custom behaviors for elements would be nice.
Reinvent the wheel
If there is a plugin that does this kind of thing, then let me know! I'm unable to find anyone ...
Would you like to focus on event handling .change ()
as event your entry point Handler can see the form element id and value in a JSON structure to determine if the work should be done, if any. Javascript can look like this:
form = {"input1": {"value1": {/ * action list * / [{"hide": ["input2", "input3 {"Input 5": "true", "input 6": "wrong"}]}, "value 2": {{/ * tasks]: {"Input 4"}: "set" Another set of * /]}} function onchange (e) {id = $ (this) .attr ("id"); Value = $ (this) .val (); {$ (Action in "id"} {$ (in action [id]) {$ .ee (form [id] [value], function (i, action) {if ("hide" in action) $ {action ) Hide ();} /} etc;}}} $ {Document} .ready (function () {$ ("input") for other tasks. Change (on change);}
< / Div>
Comments
Post a Comment