繁体   English   中英

电子邮件表单互动

[英]E-mail form interactivity

我是一名网络开发专业的学生,​​我需要一些帮助。 我有下面的代码; 我如何使其仅在提交表单而不单击文本字段时起作用。 我也希望它获取并将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 -->

这里有几个问题:

通过使用$('.quote').click() ,您可以为<form>包含的任何元素上的任何 click事件设置处理程序。 如果只想捕获提交事件,则应在“提交”按钮上设置一个单击处理程序:

// BTW, don't use an id like "button" - it'll cause confusion sooner or later
$('#button').click(function() { 
    // do stuff
    return false; // this will keep the form from actually submitting to the server,
                  // which would cause a page reload and kill the rest of your JS
});

或者,最好是以下形式的提交处理程序:

// reference by id - it's faster and won't accidentally find multiple elements
$('#quote').submit(function() { 
    // do stuff
    return false; // as above
});

提交处理程序更好,因为它们捕获了其他提交表单的方法,例如在文本输入中单击Enter

另外,在隐藏的<div> ,您以纯文本而不是<script>标记的形式放入Javascript,因此这将在屏幕上可见。 您可能想要一个可以引用的占位符元素:

<div class="thanks">Thanks for contacting us <span id="nameholder"></span>, we'll get back to you as soon as possible</div>

然后,您可以将名称粘贴到占位符中:

var name = $("#name").val();
$('#nameholder').html(name);

我不知道您要使用$("input").val(text);行做什么$("input").val(text); -这里没有定义text ,所以这实际上没有任何意义。

这有点粗糙并且已经准备好,但是应该可以帮助您

$(document).ready(function(){
  $("#submitbutton").click(function(){
    //fade out the form - provide callback function so fadein occurs once fadeout has finished
    $("#theForm").fadeOut(500, function () {
        //set the text of the thanks div
        $("#thanks").text("Thanks for contacting us " + $("#name").val());
        //fade in the new div
        $("#thanks").fadeIn(600);
        });

  }); 

});

我改变了一些HTML:

<div id="theForm">
<form action="" method="get" id="quote" class="quote">
  <p>
    <label>
      <input type="text" name="name" id="name" />
    </label>
  </p>
  <p>
    <label>
      <input type="button" name="submitbutton" id="submitbutton" value="Submit" />
    </label>
  </p>
</form>
</div>
<div id="thanks">Thanks for contacting us, we'll get back to you as soon as posible</div><!-- End thanks -->

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM