简体   繁体   English

jQuery.ajax无法启动

[英]jQuery.ajax does not fire

I'm new to jQuery so bear with me :-) 我是jQuery的新手,请忍受我:-)

What i'm trying to do is to validate user input in a form, checking the input against mysql database. 我想做的是验证用户输入的形式,对照mysql数据库检查输入。 Then set the color depending on T/F. 然后根据T / F设置颜色。

I have managed to get this working on a test page, but on my real page it does not work :( 我已经设法在测试页上运行此功能,但在我的真实页面上它不起作用:(

This is the working code. 这是工作代码。

<style>
body{width:50%;}
#frmCheckUsername {border-top:#F0F0F0 2px solid;background:#FAF8F8;padding:10px;}
.demoInputBox{padding:7px; border:#F0F0F0 1px solid; border-radius:4px;}
.status-available{color:#2FC332;}
.status-not-available{color:#D60202;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>


$(document).ready(function(){
    $("#username").keyup(function(){
    //alert( "Handler for .keyup() called." );
    checkAvailability();
    });
});

function checkAvailability() {
    //$("#loaderIcon").show();
    jQuery.ajax({
    url: "check_av2.php",
    data:'username='+$("#username").val(),
    type: "POST",
    success:function(data){
        //alert( data );
            //if(data){$("#username").css("background-color", "yellow");}
        $("#username").css("background-color", data);
    },
    error:function (){}
    });
}
</script>


<label>Check Username:</label>
<input name="usernae" type="text" id="username" class="demoInputBox">  


----
check_av2.php

<?php
include ('inc/environment.php');
db_open();

$id = $_POST["username"];

if(!empty($id)) {
    $result = mysql_query("SELECT id FROM lakes WHERE id='".$id."'");
    $row = mysql_num_rows($result);
    db_close();
    if($row>0) {
    echo "#BAF490";

    }else{
    echo "#F22A26";
    }
}
?>

Now when copying this to the existing code it will not change the color. 现在,将其复制到现有代码时,不会更改颜色。 I don't know how to proceed now. 我现在不知道该如何进行。 Does 'div' affect the id's? “ div”会影响ID吗? The existing code also includes other scripts 现有代码还包括其他脚本

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="./fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
<script type="text/javascript" src="./fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" type="text/css" href="./fancybox/jquery.fancybox-1.3.4.css" media="screen" />

Do these other script includes interfere? 这些其他脚本是否包含干扰?

Here the code that does not work. 这里的代码不起作用。

<SCRIPT>
$(document).ready(function(){
$("#sm").keyup(function(){
  //alert( "Handler for .keyup() called." );
  checkAvailability();
});
});

function checkAvailability() {
  //$("#sm").css("background-color", "yellow");
  jQuery.ajax({
  url: "check_av2.php",
  data:'username='+$("#sm").val(),
  type: "POST",
  success:function(data){
        //alert( data );
        $("#rstm").css("background-color", "yellow");
        $("#sm").css("background-color", data);
        //$("#loaderIcon").hide();
  },
  error:function (){}
  });
}
</SCRIPT>   
<input name="sm"
type="text" class="uppercase"
id="sm" value="<?=$sm?>" size="4"
title="Eget">

Now if i un-comment $("#sm").css("background-color", "yellow"); 现在,如果我取消注释$(“#sm”)。css(“ background-color”,“ yellow”); the box will become yellow typeing in the box, but jquery.ajax does not seem to fire. 该框将变成黄色,但jquery.ajax似乎没有触发。

Not so easy to explain, but i hope you get it :-) And i can't post the whole code for the not working page here. 解释起来不太容易,但我希望您能理解:-)而且我不能在此处发布整个无法正常工作的页面的代码。 I could send the code on mail if needed. 如果需要,我可以通过邮件发送代码。

SOLVED I could not let this go past today. 解决了,我今天不能过去。 The solution if on of the more annoying. 解决的办法就比较烦人了。 Found it by removing code in blocks and narrow the blocks. 通过删除块中的代码并缩小块来找到它。 A BIG varning to 一个巨大的变化

<meta http-equiv="X-UA-Compatible" content="IE=9" />

that was present in the code. 代码中存在的内容。 Removing that line did the trick!! 删除那条线就可以了!! Tanks anyway :-) 反正坦克:-)

Please Try 请试试

data:{'username':$("#username").val()}

Good work :) 做得好:)

Please pass an object as data 请传递一个对象作为数据

jQuery.ajax({
    url: "check_av2.php",
    data:{'username':$("#username").val()},
    type: "POST",
    success:function(data){
        $("#username").css("background-color", data);
    },
    error:function (){}
    });


jQuery.ajax({
    url: "check_av2.php",
    data:{'username':$("#sm").val()},
    type: "POST",
    success:function(data){
        $("#rstm").css("background-color", "yellow");
        $("#sm").css("background-color", data);
    },
    error:function (){}
});

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

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