使用 jQuery,如何强制访问者滚动到文本区域的底部以启用提交按钮?
我有一个注册表,其中包含只读文本区域。我想要求任何访问者在启用提交按钮提交信息之前滚动到文本区域的底部,其中包含条款和条件或许可协议。
这是示例 CSS 代码:
textarea {
width: 240px;
height: 200px;
overflow-y: scroll;
}
这是示例 HTML 代码:
<form action="action.php">
<label for="email">Email Address</label><input type="text" id="email" />
<textarea readonly>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
<input class="submit" type="submit" value="Register your info" id="register"/>
</form>
是否应该将 disabled
属性放入提交输入中?
我们已经在使用 jQuery 库,因此我想继续使用 jQuery 来启用此验证来控制提交问题。感谢您的帮助和建议!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样的事情应该可行:
只需给术语一个 id,然后在 html 中将注册按钮设置为禁用。我还做了一些小提琴来展示它的工作原理: http://jsfiddle.net/ETLZ8/
Something like this should work:
Simply give terms an id, and set the register button to disabled in the html. I also made a little fiddle to show it working: http://jsfiddle.net/ETLZ8/
我更推荐这个,它可以更好地处理缩放。
当scrolltop+innerheight略低于scrollHeight时,+2可以处理一些缩放场景(出于某种原因,我懒得解决)。
I recommend this rather, it handles zooming better.
The +2 handles a few scaling scenarios when scrolltop+innerheight is marginally below scrollHeight (for some reason I am too lazy to work out).
Christian Varga 的解决方案 绝对正确,也可以应用于 div。但是,如果您使用 div 而不是文本区域,则 div 上不得有任何填充,否则会损坏。
我的解决方法是在我的样式 div 中放置一个 div(带有填充、圆角和边框)来检测滚动。因此:
这种方法唯一可能的缺点是,如果您向包含的 div 添加填充,滚动条会出现在内部 div 上,这对某些用户来说可能看起来不太好。
Christian Varga's solution is absolutely correct, and can also be applied to a div. However, if you are using a div instead of a textarea, the div MUST NOT have any padding on it or it breaks.
My workaround for this was to place a div inside my styled div (with padding, rounded corners, and a border) to detect scrolling. So:
The only possible drawback to this approach is if you add padding to the containing div, the scrollbar appears on the inner div, which may not look good to some users.
其他答案完美地工作,但我使用稍微不同的方法整理了一个示例,该方法不不要将提交功能与禁用的按钮联系起来,以便它可以与其他验证器等混合。
HTML:
Other answers work perfectly, but I've put together a sample using a slightly different approach, one that doesn't tie the ability to submit to the button being disabled, so that it can be mixed with other validators and such.
HTML:
在文本区域中使用类似这样的内容:
但是如果 JS 被禁用怎么办?
我更喜欢底部有提交按钮的可滚动 div。无论 JS 开启还是关闭,用户都无法在最后不滚动的情况下点击按钮。
Use something like this inside the textarea:
But what if JS is disabled?
I would prefer a scrollable div with the submit-button at the bottom. The user can't click the button without scrolling at the end, no matter if JS is on or off.