jQuery: Executing functions in previously saved data and online?
i've been working a little time with jQuery and i can't figure out how to do this...
The thing is in work i'm making a lot开发者_C百科 of forms that can be reloaded with different data and events width dependents controls. For example (a simple example) we have 3 radiobuttons to select the favorite food: Meat, Fish and Another. When they click the another radiobutton they can specify what is this other food in a textbox (and the other two disable this textbox)
They can save the data and they can reopen this form with the data loaded and the same input status (disabled/enabled).
Normally we program this in 2 parts, one that executes when the page is loaded (this part enables/disables the inputs according to the previously saved data - or the lack of data 'if the another radiobutton is selected enable the specify textbox, else disable it') and one that executes when the click event is triggered (if they click the another radiobutton enable the textbox, if they click another one disable it - and this is an onclick function for every radiobutton)
I've been thinking a lot and I think that could be a way to do this by jquery in one time, but I haven't found the way to do it... any ideas?
I know you could think that doing this in 2 steps isn't a lot of work, but you should know that we're working with forms with more than 200 different dependent inputs... that's the reason because i would like to improve and reduce the js code.
ah! we work in asp.net (c#), so if you have any idea to do this with asp.net controls instead of jQuery, it will also be welcomed.
Thanks a lot, and sorry about my english -.-
JSON is the answer, why: 1.It can pack real parameters into just one options parameter : for example: {"id":"me","opened":1,"used":{"user":"kiki","rights":"none"}} 2.this can be sent to your server side code of your choice and stored/interpreted/retransmited to jvascript later
All you need to do is establish your parameters wisely. (I've made a "table" select box with jQuery which can have multiple columns,predefined number of rows,paging (database linked, meaning it requests another page from serverside when needed, parametrized fields for searching (also linked to server side) +++ keyboard navigation +++ it is lightspeed, all with JSON). There is a simple structured JSON code that moves back and forth between serverside and jQuery.
Well, in addition to json.. This is with reference to the following part of your problem.
"If they click the another radiobutton enable the textbox, if they click another one disable it - and this is an onclick function for every radiobutton"
Suppose, for some markup like..
<span class="data">
<input type="radio" value="1" /> One
<input value="one"/>
</span>
You can attach the toggle behavior as follows..
$().ready(function(){
$('.data :radio').each(function(){
$(this).live('click', function(){
if($(this).is(":checked")){
$(this).next(":input")..removeAttr('disabled');
}
else{
$(this).next(":input").attr('disabled', true);
}
});
});
});
精彩评论