简体   繁体   English

使用JQuery的AJAX中最简单的脚本不起作用

[英]easiest script in AJAX with JQuery doesn't work

im tring to learn using ajax but no one of my script work: 我想学习使用ajax,但我的脚本没有工作:

<script>
function test()
{   
    var par = document.getElementById("nome").value;
    alert ("i m here!"+par);
    $.Ajax({
      type: "POST",
      url: "ProvaAJAX.php",
      data: "par="+par,
      success: function(msg){
       alert( "Data Saved: " + msg );
      }
    });
}
</script>

Select Item: 选择物品:

<select id="nazione_pr" class="select-registrazione" onchange="prova()" name="nazione_pr">

Input item: 输入项目:

<input id="nome" class="input-text" type="text"name="nome">

ProvaAJAX.php ProvaAJAX.php

<html>
<?php
    echo "Test success". $_POST['par'];
?>
</html>

.... i think this script should be ok but my server doesn't think this... when i change the "select" value the "im here" alert box appear but after nothing happen... ....我认为这个脚本应该没问题,但我的服务器不认为这...当我更改“选择”值时,“我在这里”警告框出现但是什么都没发生......

Many issues 很多问题

  • It's lowercase a in $.ajax 它是$ .ajax中的小写字母

  • data: {"par":par}, 数据:{“par”:par},

  • you use jQuery so use var par = $("#nome").val(); 你使用jQuery所以使用var par = $(“#nome”)。val();

  • your function is called something else than what you call it 你的函数被称为别的东西而不是你所说的

Hit F12 in IE or Chrome or install firebug in Firefox and hit F12 there too to see the console 在IE或Chrome中点击F12或在Firefox中安装firebug并在那里点击F12以查看控制台

<script>
$(function() { // when the page has loaded
 $("#nazione_pr").on("change",function() {
    var nazione = $(this).val(); // the select's value
    var par =$("#nome").val();  // the textfield's value
    $.ajax({
      type: "POST",
      url: "ProvaAJAX.php",
      data: {"par":par,"nazione":nazione},
      success: function(msg){
       alert( "Data Saved: " + msg );
      }
    });
  });
});
</script>

please change onchange="prova()" to onchange="test()" 请将onchange =“prova()”更改为onchange =“test()”

<script>
    function test()
    {   
       var par = document.getElementById("nome").value;
        $.ajax({
            type: "POST",
            url: "ProvaAJAX.php",
            data: { par:par,fun:"test" }
            }).done(function( data ) 
            {
                alert( "Data Saved: " + data );
            });

    }
    </script>

and php file:- 和PHP文件: -

if($_REQUEST['fun'] == "test")
{
 echo $_REQUEST['par'];
}
            // Launch AJAX request.
            $.ajax(
                {
                    // The link we are accessing.
                    url: jLink.attr( "href" ),

                    // The type of request.
                    type: "get",

                    // The type of data that is getting returned.
                    dataType: "html",

                    error: function(){
                        ShowStatus( "AJAX - error()" );

                        // Load the content in to the page.
                        jContent.html( "<p>Page Not Found!!</p>" );
                    },

                    beforeSend: function(){
                        ShowStatus( "AJAX - beforeSend()" );
                    },

                    complete: function(){
                        ShowStatus( "AJAX - complete()" );
                    },

                    success: function( strData ){
                        ShowStatus( "AJAX - success()" );

                        // Load the content in to the page.
                        jContent.html( strData );
                    }
                }                           
                );

            // Prevent default click.
            return( false );                    
        }
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM