2017-05-20 06:41
浏览 74


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?

        <title>AJAX TUTORIAL</title>
        <script src= "jqueryy.js"></script>
        //var user_email=$("#email").val();
            <div id="box">
            <input type="text" name="email" id="email"/>
            <input type="submit" name="sub" id="sub" value="Submit"/>
            <div id="result">



图片转代码服务由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; 
  // 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 .


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

    <script src=""></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){
        //Your code here

    For the form:

    1: Wrap your inputs in a tag

    2: This code bit

    $('form').on('submit', function(e){
        //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.

    打赏 评论

相关推荐 更多相似问题