-1

我只是摆弄了以下脚本。它不工作。

<SCRIPT> 

function kin() {
    var kobj = document.getElementById("k"); // input Object 
    var q = kobj.value; 

       $.ajax({
      type: "POST",
      URL: "zips.php",
      data:q,
      success: function(res) {
            $("#result").append(res);
            console.log(res);
      }
   });

}

</SCRIPT> 

 <INPUT type="text" autocomplete="off" value="" id="k"><input type="submit" value="search" onClick="kin()" data-role="button">      
<DIV id="result"></DIV>
4

2 回答 2

1

代替:

data:q,

尝试:

data:{"q":q},
于 2012-06-18T22:34:15.503 回答
1

我注意到您发布的代码中有两个错误。我评论了旧代码,所以你可以很容易地注意到变化。

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

<SCRIPT> 

function kin() {
    //var kobj = document.getElementById("k"); // input Object 
    //var q = kobj.value; 

    var q = $("#k").val();

       $.ajax({
      type: "POST",
      url: "zips.php",
      //data:q,
      data: "q="+q,
      success: function(res) {
            $("#result").append(res);
            console.log(res);
      }
   });

}

</SCRIPT> 

 <INPUT type="text" autocomplete="off" value="" id="k"><input type="submit" value="search" onClick="kin()" data-role="button">      
<DIV id="result"></DIV>

JQuery 允许您像这样重写您发布的代码:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<SCRIPT> 

$(document).ready(function() { 
    $("#submit").click(function(e) {
      e.preventDefault();

      var q = $("#k").val();

      $.ajax({
          type: "POST",
          url: "zips.php",
          data: "q="+ q,
          success: function(res) {
                $("#result").append(res);
                console.log(res);
        },
      });

    });       

});

</SCRIPT> 

 <INPUT type="text" autocomplete="off" value="" id="k"><input type="submit" id="submit" value="search" data-role="button">      
<DIV id="result"></DIV>
于 2012-06-19T00:15:34.960 回答