电子邮件表单交互
我是一名网络开发学生,我需要一些帮助。我有下面的代码;如何使其仅在提交表单而不是单击文本字段时才起作用。我还希望它能够获取 textField 的值并将其插入 .thanks Div 中。请帮助我学习。
<script type="text/javascript">
$(document).ready(function(){
$(".quote").click(function(){
$(this).fadeOut(5000);
$(".thanks").fadeIn(6000);
var name = $("#name").val();
$("input").val(text);
});
});
</script>
<style type="text/css">
<!--
.thanks {
display: none;
}
-->
</style>
</head>
<body>
<form action="" method="get" id="quote" class="quote">
<p>
<label>
<input type="text" name="name" id="name" />
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>
</form>
<div class="thanks"> $("#name").val(); Thanks for contacting us, we'll get back to you as soon as posible</div><!-- End thanks -->
I'm a web development student and I need some help. I have the code below; How do I make it work only when the form is submitted and not the text field is clicked. I also would like it to get and insert the textField's value in the .thanks Div. Please help me learn.
<script type="text/javascript">
$(document).ready(function(){
$(".quote").click(function(){
$(this).fadeOut(5000);
$(".thanks").fadeIn(6000);
var name = $("#name").val();
$("input").val(text);
});
});
</script>
<style type="text/css">
<!--
.thanks {
display: none;
}
-->
</style>
</head>
<body>
<form action="" method="get" id="quote" class="quote">
<p>
<label>
<input type="text" name="name" id="name" />
</label>
</p>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>
</form>
<div class="thanks"> $("#name").val(); Thanks for contacting us, we'll get back to you as soon as posible</div><!-- End thanks -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点粗糙,但应该能让你继续下去
,我对 html 做了一些修改:
This is a bit rough and ready but should get you going
and I changed the html a bit:
这里有几个问题:
通过使用
$('.quote').click()
,您可以在包含的任何元素上的任何点击事件上设置处理程序在或者最好在表单上设置提交处理程序:
提交处理程序更好,因为它们捕获提交表单的其他方式,例如点击
在文本输入中输入
。另外,在隐藏的
中,您以纯文本形式放入 Javascript,而不是在
标记中,因此它是可见的在屏幕上。您可能需要一个可以引用的占位符元素:
然后您可以将名称粘贴到占位符中:
我不知道您要对行
$("input").val(text); 做什么
- 此处未定义text
,因此这实际上没有任何意义。There are several things at issue here:
By using
$('.quote').click()
, you're setting a handler on any click event on any element contained within the<form>
. If you want to catch only submit events, you should either set a click handler on the submit button:or, preferably, a submit handler on the form:
Submit handlers are better because they catch other ways of submitting a form, e.g. hitting
Enter
in a text input.Also, in your hidden
<div>
, you're putting in Javascript in plain text, not in a<script>
tag, so that's just going to be visible on the screen. You probably want a placeholder element you can reference:Then you can stick the name into the placeholder:
I don't know what you're trying to do with the line
$("input").val(text);
-text
isn't defined here, so this doesn't really make any sense.