我正在尝试这样做,以便如果用户将焦点设置为表单元素 (文本),它会自动将焦点设置为下一个表单元素。实际上,这个 会阻止用户修改该值。我知道我可以通过将表单元素设置为''readonly'来做 ,但这是不可能的,因为 我需要修改该值在我的JS其他地方。无论如何... 我知道我可以这样做: document.forms [1] .elements [4] .focus ()。 上述方法对我来说唯一的问题是我动态生成我的表格 所以我不知道a)当前字段是哪个元素编号和 b)哪个是下一个元素,如果有的话。我已经尝试过设置一个活动 处理程序所以: OnFocus =''this.nextSibling.focus();'' 但这不起作用。显然,在使用表单元素工作时,我不能使用nextSibling。所以我想知道如何做我需要做的事情? 任何帮助都将不胜感激! thnx, Chris
I''m trying to make it so that if the user sets focus to a form element (text), it automatically sets focus to the very next form element. This will, in effect, prevent the user from modifying the value. I know I can do this by setting the form element to ''readonly'' but that''s not possible as I''ll need to modify that value elsewhere in my JS. Anyway... I know I can do something like this: document.forms[1].elements[4].focus(). Only problem for me with the above method is that I''m generating my form dynamically so I won''t know a) which element number the current field is and b) which, if any, is the next element. I''ve tried setting up an event handler so: OnFocus=''this.nextSibling.focus();'' but that doesn''t work. Apparently, I can''t use nextSibling when working with a form element. So I''m wondering how I can do what I''m needing to do? Any help would be greatly appreciated! thnx, Chris
推荐答案Christoph于2006年1月30日在comp.lang.javascript中写道 : Christoph wrote on 30 jan 2006 in comp.lang.javascript: 我知道我可以通过将表单元素设置为''readonly'来做到这一点,但这是不可能的因为我''我需要在我的JS中的其他地方修改该值。 I know I can do this by setting the form element to ''readonly'' but that''s not possible as I''ll need to modify that value elsewhere in my JS.
1你可以写一个readonly< input>的值使用javascript的元素 2你不能改变< input>的readonlyness使用javascript元素 3但是,你可以使用禁用对你有利。 <输入id = x禁用> < script type =" text / javascript"> var x = document.getElementById(''x'') x.value =''hi world'' alert(x。禁用) x.disabled = false < / script> 不要乱用focus()来限制用户! - Evertjan。 荷兰。 (请更改x 我的电子邮件中的点数
1 you can write the value of a readonly <input> element with javascript 2 you cannot change the readonlyness of an <input> element with javascript 3 however, you can use "disabled" to your advantage. <input id=x disabled> <script type="text/javascript"> var x = document.getElementById(''x'') x.value = ''hi world'' alert(x.disabled) x.disabled = false </script> Do not mess with focus() to restrain the user! -- Evertjan. The Netherlands. (Please change the x''es to dots in my emailaddress)
Christoph写道: Christoph wrote: [...] 只有问题对我来说,上面的方法是我动态地生成我的表格,所以我不知道a)当前字段是哪个元素编号和b / b)哪个,如果任何,是下一个元素。我已经尝试过设置一个事件处理程序: OnFocus =''this.nextSibling.focus();'' 但是没有不行。 它不起作用,因为`this.nextSibling''不是指一个元素 对象代表一个表单控件但可能是一个TextNode对象 没有这样的方法。 显然,在处理表单元素时我不能使用nextSibling。 你可以。 所以我想知道如何做我需要做的事情? [...] Only problem for me with the above method is that I''m generating my form dynamically so I won''t know a) which element number the current field is and b) which, if any, is the next element. I''ve tried setting up an event handler so: OnFocus=''this.nextSibling.focus();'' but that doesn''t work. It does not work because `this.nextSibling'' does not refer to an Element object that represents a form control but probably a TextNode object that has no such method. Apparently, I can''t use nextSibling when working with a form element. You can. So I''m wondering how I can do what I''m needing to do?
迭代,直到遇到代表表单控件的节点。 Quickhack: 函数isMethodType(s) { return(s ==" function" || s ==" object"); } 函数getNextControl(o) { if(o) { while( (o = o.nextSibling)) { if(isMethodType(typeof o.focus) && / input | button | select / .test(o.tagName.toLowerCase())) { 返回o; } } } 返回{focus:function(){}}; } < ... onfocus =" getNextControl(this).focus();" PointedEars
Iterate until you encounter a node that represents a form control. Quickhack: function isMethodType(s) { return (s == "function" || s == "object"); } function getNextControl(o) { if (o) { while ((o = o.nextSibling)) { if (isMethodType(typeof o.focus) && /input|button|select/.test(o.tagName.toLowerCase())) { return o; } } } return {focus: function() {}}; } <... onfocus="getNextControl(this).focus();" PointedEars
> " Evertjan"。 <前************** @ interxnl>写道: > "Evertjan." <ex**************@interxnl> wrote: 新闻:Xn ******************** @ 194.109.133.242 .... Christoph写于2006年1月30日的comp.lang.javascript : news:Xn********************@194.109.133.242.... Christoph wrote on 30 jan 2006 in comp.lang.javascript: 我知道我可以通过将表单元素设置为''readonly来实现这一点''但这不是可能的,因为我需要在我的JS中的其他地方修改该值。 1你可以写一个readonly< input>的值。元素与 javascript 2你不能改变< input>的readonlyness元素与 javascript I know I can do this by setting the form element to ''readonly'' but that''s not possible as I''ll need to modify that value elsewhere in my JS. 1 you can write the value of a readonly <input> element with javascript 2 you cannot change the readonlyness of an <input> element with javascript
我以为可以在true和false之间切换readOnly。 会出现什么情况是这不起作用吗? 3但是,你可以使用禁用对你有利。 <输入id = x禁用> < button type =" button"的onclick = QUOT; x.readOnly =(x.readOnly)假:真; x.focus(); x.select();"> toggle readOnly< / button> < script type =" text / javascript"> var x = document.getElementById('' x'') x.value =''hi world'' alert(x.disabled) x.disabled = false < / script> 不要乱用focus()来约束用户!
I thought one could toggle readOnly between true and false. What would the situation be that this does not work in? 3 however, you can use "disabled" to your advantage. <input id=x disabled> <button type="button" onclick="x.readOnly=(x.readOnly)?false:true; x.focus(); x.select();">toggle readOnly</button> <script type="text/javascript"> var x = document.getElementById(''x'') x.value = ''hi world'' alert(x.disabled) x.disabled = false </script> Do not mess with focus() to restrain the user!
- BootNic 2006年1月31日星期二12 :53 PM 告知所有通信已完全崩溃的部队。 * Ashleigh Brilliant *
-- BootNic Tuesday, January 31, 2006 12:53 PM Inform all the troops that communications have completely broken down. *Ashleigh Brilliant*
更多推荐
下一个表单元素
发布评论