dongxuxian1123
2017-05-20 06:41
浏览 74

使用提交按钮时,Ajax调用不起作用

Below is the simple code to call the ajax but when I click submit button,it doesn't work. I did echo "hello world"; in second file "test.php". Can someone get me out of this?

<html>
        <head>
        <title>AJAX TUTORIAL</title>
        <script src= "jqueryy.js"></script>
        <script>
        $(document).ready(function(){
        $("#sub").click(function(){
        //var user_email=$("#email").val();
        $.post("test.php",function(data){
        $("#result").html(data);
        });
        });
        });
        </script>
        </head>
            <body>
            <div id="box">
            <input type="text" name="email" id="email"/>
            <input type="submit" name="sub" id="sub" value="Submit"/>
            <div id="result">

            </div>
            </div>

            </body>
    </html>

图片转代码服务由CSDN问答提供 功能建议

下面是调用ajax的简单代码,但是当我单击“提交”按钮时,它不起作用。 我确实回应了“你好世界”; 在第二个文件“test.php”中。 有人可以让我离开吗?

 &lt; html&gt; 
&lt; head&gt; 
&lt; title&gt; AJAX TUTORIAL&lt; / title&gt; 
&lt; script src  =“jqueryy.js”&gt;&lt; / script&gt; 
&lt; script&gt; 
 $(document).ready(function(){
 $(“#sub”)。click(function(){
  // var user_email = $(“#email”)。val(); 
 $ .post(“test.php”,function(data){
 $(“#result”)。html(data); \  n}); 
}); 
}); 
&lt; / script&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; div id =“box”&gt; 
&lt;  input type =“text”name =“email”id =“email”/&gt; 
&lt; input type =“submit”name =“sub”id =“sub”value =“Submit”/&gt; 
&lt;  ; div id =“result”&gt; 
 
&lt; / div&gt; 
&lt; / div&gt; 
 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanjiong2021 2017-05-20 07:04
    已采纳

    You don't have any form tag .so it will not reload the page when submit is clicked I think your code seems good .

    1) May be the problem is jquery included file name typo error jqueryy.js at the end double yy .

    or

    2) remove that link and replace with this cdn link and try

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    
    打赏 评论
  • douliedu335997 2017-05-20 06:44

    Your question already states the answer, Ajax call doesnot work when submit button is used. Because submit button submit the form to the server and it reloads the page, that's why your ajax do not initiates.

    So you have to hold the form submission or change button from submit to button.

    But there is no form in your code. And you are including jquery like:

    <script src= "jqueryy.js"></script>
    

    please check if there is a js file with the name jqueryy.js present in the specified path.

    打赏 评论
  • duanaoou4105 2017-05-20 06:55

    You can prevent the from from submitting by either waiting on the submit event for the form, or click listener for the button

    For the button:

    $('#sub').on('click', function(e){
        e.preventDefault();
    
        //Your code here
    });
    

    For the form:

    1: Wrap your inputs in a tag

    2: This code bit

    $('form').on('submit', function(e){
        e.preventDefault();
        //Your Code here
    })
    

    On both of these, you have the e.preventDefault(); In short, it prevents the default action of your selector. In this case the form submission.

    打赏 评论

相关推荐 更多相似问题