表单仍然提交返回false

时间:2013-09-08 15:24:25

标签: php jquery html

我想要它,以便如果人们点击发送按钮,那么表格的提交应该停止。

我的目的是提交信息,但让用户保持在同一页面上。

我的JS:

$(document).ready(function () {
    $("#kontakt").submit(function () {
        return false;
    });

HTML:

<form id="kontakt" method="post" action="send.php">
    <br>
    <input type="text" name="von" id="von" placeholder="name" />
    <br>
    <input type="text" id="mail" name="mail" placeholder="email" />
    <br>
    <input type="text" id="betreff" id="betreff " placeholder="betreff" />
    <br>
    <textarea style="width:500px;height:400px;" id="nachricht" name="nachricht"></textarea>
    <input id="submit" type="submit" value="senden"></input>
</form>
<div id? "response"></div>

小提琴:http://jsfiddle.net/gF6wg/

问题是,如果您点击“发送”,表单仍在提交。

4 个答案:

答案 0 :(得分:2)

你错过了)};

$(document).ready(function () {
    $("#kontakt").submit(function () {
        return false;
    });
)}; // <-- this

你也应该改变这个:

<div id="response"></div>
       ^ '=' instead of '?'

这绝对有效,请在此处试试:http://jsfiddle.net/gF6wg/3/

更新:这是完整页面,只需将其复制到您的html文件中:http://pastebin.com/CRzKgVhk

以下是一些可用于使用ajax提交表单的简单代码:

$(document).ready(function () {
    $("#kontakt").submit(function () {
        var empty = $(this).find("input[type=text], textarea").filter(function () {
            return $.trim(this.value).length === 0;
        }).length;
        alert(empty);
        if (empty !== 0) {
            $("#response").html("Bitte alles schreiben");
        } else {
            $.post("send.php", $(this).serialize(), function(data){
                $("#response").html(data);
            });
        }
        return false;
    });
});

以下是此代码的演示:http://jsfiddle.net/mrNHy/1/

答案 1 :(得分:2)

您忘记关闭准备好的功能了。正确的JS应该是这样的:

$(document).ready(function () {
    $("#kontakt").submit(function () {
        return false;
    });
});

您可以使用Mozilla Firefox 错误控制台检查Javascript错误(您可以通过按热键 ctrl + shift + J 在Firefox中显示错误)

答案 2 :(得分:0)

正在使用 DEMO

试试这个

<form id="kontakt" method="post" action="send.php">
    <br>
    <input type="text" name="von" id="von" placeholder="name" />
    <br>
    <input type="text" id="mail" name="mail" placeholder="email" />
    <br>
    <input type="text" id="betreff" id="betreff " placeholder="betreff" />
    <br>
    <textarea style="width:500px;height:400px;" id="nachricht" name="nachricht"></textarea>
    <input id="submit" type="button" value="senden"></input> <!--change type from 'submit' to 'button'-->
</form>
<div id="response"></div>

 $(document).ready(function () {
$('#submit').click(function()
{
    $.ajax({
        url : 'send.php', 
        type: 'POST',
        data: $('#kontakt').serialize(),
        success : function(response)
        {

        $("#response").html(response);
        } 
    }); 
});

});
你的send.php上的

添加了这个

<?php
  $data=$_POST['serialize'];
   $name=$data['von']; // acceess the data like this
   // do your code


?>

答案 3 :(得分:0)

试试这个:

<script>
$("#kontakt").submit(function(event) {
 if(!event.cancelable){
  event.cancelable=true;
 }
  event.preventDefault();
  var $form = $(this),
      von = $form.find( 'input[name="von"]' ).val(),
      mail = $form.find( 'input[name="mail"]' ).val(),
      betref = $form.find( 'input[name="betref"]' ).val(),
      url = $form.attr('action');

  /* Send the data using post */
  var posting = $.post( url, { von: von, mail: mail, betref: betref } );

  /* Put the results in a div */
  posting.done(function( data ) {
    $( "#result" ).empty().append( data );
  });
});
</script>
相关问题