PDA

View Full Version : A javascript problem, help me!!!



lihang
05-09-2006, 06:32 AM
Today, I meet a problem.

A checkbox and a text in a page, if I want the text is disabled unless the checkbox is checked, how can I write that javascript?

Anyone who can help me?

jscheuer1
05-09-2006, 07:19 AM
<form action="">
<input type="text" name="tr" disabled="1"><input type="checkbox" onclick="if (this.checked){this.form.tr.disabled=0}else{this.form.tr.disabled=1}">
</form>

lihang
05-10-2006, 07:09 AM
Aha!, that works.

Thank you, jscheuer1 !!!

Twey
05-10-2006, 04:36 PM
<form action="">method is required.

mwinter
05-10-2006, 06:11 PM
<form action="">method is required.No, it isn't. :) It defaults to the GET request method. Only the action attribute is required, though an empty value might not be a good idea: IE resolves empty href attributes incorrectly, so it's feasible that others will have similar issues not restricted to that particular attribute.

Mike

Twey
05-10-2006, 06:51 PM
Oh, so it isn't. My apologies -- could have sworn I'd had trouble validating that before.

jscheuer1
05-10-2006, 06:55 PM
Thanks, Mike. I was out to sea on this one. Only reason I included it at all was that it is in my editors' 'come with' form boiler plate. But, as previously noted in these forums, others may use the value of their choice. I suppose if no action is truly desired:


action="javascript:void(0);"

would do. I have never seen this cause a problem though, leaving it blank, but I suppose anything is possible, as you note.

Twey
05-10-2006, 07:19 PM
If that works, it will cause nothing to happen when the form is submitted. This is different to the effect of an empty action or "?", which will cause the form to be submitted to the current page.

jscheuer1
05-10-2006, 07:35 PM
Well, we're talking here about a form that never gets submitted. I would think that if a user were to adapt the code to a form that does get submitted, they would have to set the values that impact submission behavior appropriately to their objectives. Submission or lack thereof has no impact on the functionality of my solution to the original question. All this is really beyond the scope of the question posed and answered originally. I think I will give Twey a nitpicking award for this one and Mike a clarification of a nit pick award though, he was mostly just responding to an obvious (to him) error on Twey's part.

Twey
05-10-2006, 08:15 PM
Heh... (wears "pedant of the year" award with pride)

jscheuer1
05-10-2006, 09:36 PM
Heh... (wears "pedant of the year" award with pride)

LOL :rolleyes:

mwinter
05-11-2006, 12:23 AM
I suppose if no action is truly desired...one should consider whether a form element is really necessary. One isn't required, and the getElementById method can be used to obtain a reference to any of the controls. Those browsers that don't implement the method can fallback to whatever provisions would be used by browsers without scripting (assuming the design is robust enough to cope).


I have never seen this cause a problem though, leaving it blank, but I suppose anything is possible, as you note.It probably won't; IE can resolve empty action attributes properly, but not empty href attributes for some reason. Whether some other user agent handles the former badly may depend upon why IE screws up with the latter.

I've never seen a problem either, though it doesn't hurt to include a URI explicitly.

Mike