Jquery ajax调用click事件提交按钮 [英] Jquery ajax call click event submit button

查看:123
本文介绍了Jquery ajax调用click事件提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击提交按钮时,我将构建一个ajax调用,所以我已经包含了jquery,并且我编写了以下代码(取自jquery文档):

I've to build an ajax call when the user clicks a submit button, so i've included jquery and i've written the following code (taken from the jquery documentation):

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function(){
  $("Shareitem").click(function(e){
      e.preventDefault();
    $.ajax({type: "POST",
            url: "/imball-reagens/public/shareitem",
            data: { id: $("Shareitem").val(), access_token: $("access_token").val() },
            success:function(result){
      $("sharelink").html(result);
    }});
  });
});
</script>

Html:

<div id="sharelink"></div>
[...]
<input type="hidden" name="id" value="" id="id"></dd>
<dd id="access_token-element">
<input type="hidden" name="access_token" value="xxxxxxxx" id="access_token"></dd>
<dt id="Shareitem-label">&#160;</dt><dd id="Shareitem-element">
<input type="submit" name="Shareitem" id="Shareitem" value="UpdatedByPreviousAjaxCall"></dd></dl></form>

问题如下,执行提交操作但是ajax调用不是,所以表单正在执行请求的提交操作,而不是停留在同一页面并更新所需div的内容。

The problem is the following, the submit action is executed but the ajax call is not, so the form is performing the requested submit action instead of staying in the same page and updating the content of the required "div".

我缺少什么?我哪里错了?
提前感谢任何提示。

What am I missing? Where am I wrong? Thanks in advance for any tip.

推荐答案

您没有添加在按钮的id之前。您的jquery代码中没有正确的选择器。所以jquery永远不会在你的按钮点击中执行。它直接提交了您的表单,没有传递任何ajax请求。

You did not add # before id of the button. You do not have right selector in your jquery code. So jquery is never execute in your button click. its submitted your form directly not passing any ajax request.

参见文档: http:// api .jquery.com / category / selectors /

是你的朋友。

See documentation: http://api.jquery.com/category/selectors/
its your friend.

试试这个:

似乎 id:$(#Shareitem)。val()如果要传递

<input type="hidden" name="id" value="" id="id">

您需要更改此行:

id: $("#Shareitem").val()

by

id: $("#id").val()

所有在一起:

 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
    $(document).ready(function(){
      $("#Shareitem").click(function(e){
          e.preventDefault();
        $.ajax({type: "POST",
                url: "/imball-reagens/public/shareitem",
                data: { id: $("#Shareitem").val(), access_token: $("#access_token").val() },
                success:function(result){
          $("#sharelink").html(result);
        }});
      });
    });
    </script>

这篇关于Jquery ajax调用click事件提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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