Jquery ajax call click event submit button

后端 未结 1 925
执笔经年
执笔经年 2021-02-08 06:27

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):

         


        
相关标签:
1条回答
  • 2021-02-08 06:36

    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.

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

    Try this:

    It seems that id: $("#Shareitem").val() is wrong if you want to pass the value of

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

    you need to change this line:

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

    by

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

    All together:

     <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>
    
    0 讨论(0)
提交回复
热议问题