与表单在页面中的Ajax请求问题。 jQuery的 [英] problems with ajax request in page with form. jquery

查看:103
本文介绍了与表单在页面中的Ajax请求问题。 jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery ajax函数做了一些事情,当我在html中间放置一个表单时,我遇到了一些问题。

简而言之:我有一个被控制的虚拟表单通过ajax,当它被提交时,ajax函数完成下面的输入。 ajax提供的数据包含在窗体标签中,该标签在点击窗体外部的按钮时提交。我这样做是因为我需要以两种不同的方式发送相同的数据。

代码:

HTML

I'm making some things with jQuery ajax function, and I have a problem when I put a form in the middle of the html.
In a nutshell: I have a dummy form that is controlled by the ajax, when it is submitted the ajax function complets the inputs below. The data submited by the ajax is contained in a form tag that is submitted when a button outside the form is clicked. I made that because I need to send the same data in 2 different ways.
The code:
HTML

<input type="button" value="Exportar a Excel" id="xlsexport">
<form action="#" method="post" name="formxls" id="formxls">
    <div class="row">
        <input type="hidden" id="xls" name="xls" value="">
        <div class="item"><span class="negro">Nombre:</span></div>
        <div class="field"><input class="input" id="nombreProv" type="text" /></div>
        <div class="item"><span class="negro">CUIT:</span></div>
        <div class="field"><input class="input" id="cuitProv" type="text" /></div>
        <div class="field"><input type="image" id="btnEnviar" value="Enviar Prov." src="images/botones/mini_send.png" style="margin-top: -1px; margin-left: 8px;" ></div>
    </div>
</form>
... Some inputs below ...



jQuery :


jQuery:

$('document').ready(function(){
   $('#formxls').keypress(function(e){ // it prevents the form will send by pressing enter 
      if(e == 13){
         return false;
      }
    });
   $('#xlsexport').click(function(){
      $('#xls').val('true');
      $('#formxls').submit();
   });
   $('input#btnEnviar').click(function(){
      if($('#xls').val() == 'true') $('#xls').val('');
      $('div#selectData').empty().removeAttr('style');
      $.ajax({ 
          // ajax request code
      });


问题是:

当我通过buttomxlsexport提交表单时,一切正常,但是当我通过ajax按钮提交表单时(btnEnviar),它做它应该做的事情然后它发送表单,所以我失去了ajax加载的所有数据。

我该如何解决它?还有其他方法可以做到这一点?这是一个没有意义的行为(至少对我来说),因为我没有其他的提交比在xlsexport点击事件。

The problem is:
When I submit the form through the buttom "xlsexport", everything is fine, but then, when I submit the form through the ajax buttom ("btnEnviar"), it does what its supposed to do and then it sends the form, so I lose all the data the ajax loads.
How can I solve it? There are other ways to do this? This is a none sense behavior (at least for me), because I dont have other submit than the one in xlsexport click event.

我希望我让自己清楚,你可以帮助我解决这个问题。

I hope I made myself clear and you can help me out with this.

非常感谢!

推荐答案

其他输入字段中可能需要名称属性(只有隐藏的输入才有)。

It probably needs the name attribute in the other input fields (only the hidden input has one).

这篇关于与表单在页面中的Ajax请求问题。 jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆