使用 jquery 进行表单扩展/缩小处理
我有一个带有文本区域、输入文本和输入单选的评论表单。首先,您会看到文本区域。如果单击它,整个表单就会展开。当您在表单外部单击并且字段没有值时,表单应再次缩小为文本区域。这也有效。
问题是,当您在表单内从一个字段切换到另一个字段时,表单会缩小并扩展,如此反复。 if ($this.not(":focus"))
不会有帮助。
有什么想法吗?
HTML
<div id="commentBox">
<form action="...">
<label for="comment">Write a comment...</label>
<textarea id="comment"></textarea>
<div class="expandBox" style="display: none">
<label for="title">Title</label>
<input type="text" id="title" />
<br />
<label for="email">E-Mail *</label>
<input type="text" id="email" />
<br />
<label for="name">Newsletter</label>
<input type="radio" id="yes" name="newsletter" /><label for="yes">ja</label>
<input type="radio" id="no" name="newsletter" /><label for="no">nein</label>
<br />
<label for="name">Name *</label>
<input type="text" id="name" />
<br />
<input type="submit" value="senden" />
</div>
</form>
</div>
jQuery 片段(当然在 domready 中)
$("#commentBox").find("input,textarea").each(function() {
var $this = $(this);
var $expandBox = $this.parents("#commentBox").find(".expandBox");
$this.focus(function() {
$expandBox.slideDown(250);
});
$this.blur(function() {
if ($this.val() === "" && $this.not(":focus")) {
$expandBox.slideUp(250);
}
});
});
I have a comment form with a textarea, input text and input radio. first, you'll see the textarea. if you click in it, the whole form expand. When you click outside the form and the fields have no values, the form should reduce to the textarea again. This works as well.
The problem is, when you switch from field to field inside the form, the form reduces and expand, and again and again. The if ($this.not(":focus"))
will not help out.
Any ideas?
HTML
<div id="commentBox">
<form action="...">
<label for="comment">Write a comment...</label>
<textarea id="comment"></textarea>
<div class="expandBox" style="display: none">
<label for="title">Title</label>
<input type="text" id="title" />
<br />
<label for="email">E-Mail *</label>
<input type="text" id="email" />
<br />
<label for="name">Newsletter</label>
<input type="radio" id="yes" name="newsletter" /><label for="yes">ja</label>
<input type="radio" id="no" name="newsletter" /><label for="no">nein</label>
<br />
<label for="name">Name *</label>
<input type="text" id="name" />
<br />
<input type="submit" value="senden" />
</div>
</form>
</div>
jQuery snippet (in domready off course)
$("#commentBox").find("input,textarea").each(function() {
var $this = $(this);
var $expandBox = $this.parents("#commentBox").find(".expandBox");
$this.focus(function() {
$expandBox.slideDown(250);
});
$this.blur(function() {
if ($this.val() === "" && $this.not(":focus")) {
$expandBox.slideUp(250);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,当您在输入/文本区域之间切换时,新的焦点元素会触发
focus()
,而最后一个元素会触发blur()
。因此,要求
$expandBox
执行slideUp()
和slideDown()
。要解决此问题,您必须告诉这些动画在调用之前的动画时停止它们。这样,
slideUp()
将被调用,并立即被slideDown()
停止。为此,请使用
stop()
。The problem is that when you switch between inputs/textareas,
focus()
is triggered on the new focused element, andblur()
on the last one.$expandBox
is thus asked toslideUp()
ANDslideDown()
.To fix this problem, you have to tell these animations to stop previous animations when they're called. That way,
slideUp()
will be called, and immediately stopped byslideDown()
.Use
stop()
for that.