Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用于提交而不刷新的Jquery(无需验证)_Php_Jquery_Ajax_Smarty - Fatal编程技术网

Php 用于提交而不刷新的Jquery(无需验证)

Php 用于提交而不刷新的Jquery(无需验证),php,jquery,ajax,smarty,Php,Jquery,Ajax,Smarty,我有一个标签表单可以正常工作,通过一些服务器验证,我想添加一个Jquery来提交内容而不刷新: <form method="post" action="tags"> <div> <input type="hidden" name="id" value="getId()" /> <input type="text" name="tag" /> <input type="submit" valu

我有一个标签表单可以正常工作,通过一些服务器验证,我想添加一个Jquery来提交内容而不刷新:

<form method="post" action="tags">
  <div>
        <input type="hidden" name="id" value="getId()" />
        <input type="text" name="tag" />
        <input type="submit" value="Add" name="add" />
    </div>
</form>


任何建议都将不胜感激。

添加JQuery javascript库

将提交按钮变成一个按钮

 <button id="submitbutton" >Add</button>
添加
将ID添加到输入中

<input type="text" id="tag" name="tag" />

并将jquery添加到单击按钮

<script type="text/javascript">
    $(document).ready(function() {
     $("#submitbutton").button().click(function(){
     $.post("tags.php",{id: $("#id").val(), tag: $("#tag").val()});
  });
});
</script>

$(文档).ready(函数(){
$(“#提交按钮”)。按钮()。单击(函数()){
$.post(“tags.php”,{id:$(“#id”).val(),tag:$(“#tag”).val();
});
});
查看。使用它,您可以在不重新加载页面的情况下提交表单,如下所示:

<form id="aForm" action="target.php" method="post>
    ...
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $("#aForm").ajaxForm();
    });
</script>

$(函数(){
$('.button')。单击(函数(){
变量数据=$('form')。serializeToObject();
$.post('tags.php',data);
});
});
//将选择器的元素序列化到对象的jQuery扩展
$.fn.serializeToObject=函数(){
var o={};
var a=this.serializeArray();
$。每个(一个,函数(){
if(o[this.name]!==未定义){
如果(!o[this.name].push){
o[this.name]=[o[this.name]];
}
o[this.name].push(this.value | |“”);
}否则{
o[this.name]=this.value | |“”;
}
});
返回o;
};

应该使用所有表单变量,序列化它们以便服务器可以接收,返回值为false以便页面在提交时不刷新(停止传播并默认)

检查。谢谢Vulcan!我试过你的答案,但它让人耳目一新。表单插件已安装,id表单与您的表单相同。这可能是什么?@82din如果没有看到你的代码,我不能肯定。尝试复制表单的教程,并调试(使用一些web控制台)以确保它确实识别插件的功能。我与$symbol有冲突。我改用jQuery。谢谢!您还可以获取返回的数据,$('#form_id').ajaxForm(函数(数据){if(数据==='1'){}}。您还可以获取返回的值,
$('#form_id').ajaxForm(函数(数据){alert(数据);});
若要获取多个值,请在php中使用JSON编码,如需更多参考,请访问Thank Worth,我在文档中使用了您的函数,并使用了正确的url,它仍然会刷新。我没有收到OK消息。哎哟,那里有语法错误,请尝试此方法,使用文档就绪函数进行全面测试谢谢Gabe!我使用了您的解决方案,但它没有提交.我检查了$.post中的url,它与操作表单相同。
<form method="post" action="tags">
  <div>
        <input type="hidden" name="id" value="getId()" />
        <input type="text" name="tag" />
        <input class="button" type="button" value="Add" name="add" />
    </div>
</form>


$(function(){
   $('.button').click(function(){
       var data = $('form').serializeToObject();
       $.post('tags.php', data);
   });
});

// jQuery Extension to serialize a selector's elements to an object
$.fn.serializeToObject = function () {
    var o = {};
    var a = this.serializeArray();

    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
 $(document).ready(function() {
  $(form).submit( function() { // could use $(#submit).on('click', function(){ as well
   $.ajax({
     url: 'yourposturl',
     data: $(form).serialize(),
     Success: function() {
         alert('ok');
     }
   }); //end ajax   

  return false;
   }); //end submit()
 });