简体   繁体   English

ajax on使用php提交无刷新表单

[英]ajax on submit no refresh form using php

ok so i know this ajax works, but i cant seem to understand why it's not submitting here. 好吧所以我知道这个ajax有效,但我似乎无法理解为什么它不在这里提交。

<script type="text/javascript">
$(function(){
    $('input[type=submit]').click(function(){
        $.ajax({
            type: "POST",
            url: "postitem.php",
            data: $("#myform").serialize(),
            beforeSend: function(){
                $('#result').html('<img src="loading.gif" />');
            },
            success: function(data){
                $('#result').html(data);
            }
        });
    });
});

submit form 提交表格

<form action="" method="post" onsubmit="return false;" id="myform">

submit button 提交按钮

<input type="hidden" value="Post" name="submit" /> 
<button type="submit" title="Done" style="height:33px; width:50px">
    <img src="../../css/images/plus_25.png" />
</button>

i'm pretty something is wrong with the button, but i want to keep the way the button is because alot of my other forms use this same button, thanks if you can explain to me why click this submit button does nothing. 我的按钮很糟糕,但是我想保持按钮的方式,因为很多我的其他形式都使用相同的按钮,谢谢你能解释为什么点击这个提交按钮什么都不做。

You are selecting an <input> instead of a <button> . 您正在选择<input>而不是<button> I had success by changing your jQuery selector from this: 我通过更改你的jQuery选择器取得了成功:

$('input[type=submit]')

to this: 对此:

$('button[type=submit]')

http://jsfiddle.net/H3Bcc/ http://jsfiddle.net/H3Bcc/

Not all browsers interpret a click attached to a submit button as submitting the form so you are effectively just negating the click of that button. 并非所有浏览器都将附加到提交按钮的click解释为提交表单,因此您实际上只是否定了该按钮的单击。

Preventing a form submission should preferably be captured by attaching the submit handler to the <form> when you have a <input type="submit"> or <button type="submit> so this is the best way to assure success: 当您有<input type="submit"><button type="submit> ,最好通过将submit处理程序附加到<form>来捕获<form> <button type="submit> ,这是确保成功的最佳方法:

jQuery jQuery的

$(function(){
    $('#myform').on('submit', function(e){

        // prevent native form submission here
        e.preventDefault();

        // now do whatever you want here
        $.ajax({
            type: $(this).attr('method'), // <-- get method of form
            url: $(this).attr('action'), // <-- get action of form
            data: $(this).serialize(), // <-- serialize all fields into a string that is ready to be posted to your PHP file
            beforeSend: function(){
                $('#result').html('<img src="loading.gif" />');
            },
            success: function(data){
                $('#result').html(data);
            }
        });
    });
});

HTML HTML

<form action="postitem.php" method="POST" id="myform">
    <input type="hidden" value="Post" name="submit" /> 
    <button type="submit" title="Done" style="height:33px; width:50px">
        <img src="../../css/images/plus_25.png" />
    </button>
</form>

try this : 试试这个 :

<script type="text/javascript">
$(function(){
    $('input[type=submit]').click(function(e){
e.preventDefault(); // prevent the browser's default action of submitting 
        $.ajax({
            type: "POST",
            url: "postitem.php",
            data: $("#myform").serialize(),
            beforeSend: function(){
                $('#result').html('<img src="loading.gif" />');
            },
            success: function(data){
                $('#result').html(data);
            }
        });
    });
});

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

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