$.post 上的 jQuery addClass

发布于 2024-08-25 18:19:32 字数 1124 浏览 1 评论 0原文

我基本上是想创建一个小型注册表。如果用户名已被占用,我想添加“红色”类,如果没有则添加“绿色”类。

这里的 PHP 工作正常,并返回“YES”或“NO”来确定是否正常。

CSS:

input {

 border:1px solid #ccc;
}
.red {

 border:1px solid #c00;
}
.green {

 border:1px solid green;
 background:#afdfaf;
}

我使用的 Javascript 是:

 $("#username").change(function() { 

             var value = $("#username").val();

  if(value!= '') {
   $.post("check.php", {
    value: value
   }, function(data){

   $("#test").html(data);

    if(data=='YES') {
     $("#username").removeClass('red').addClass('green');
    } if(data=='NO') {
     $("#username").removeClass('green').addClass('red');
    }
   });
  }  
 });

我也有 document.ready 的东西...一切正常,因为除了最后一部分之外,#test div html 更改为“YES”或“NO”我在其中检查数据的价值。这是PHP:

 $value=$_POST['value'];

 if ($value!="") {
  $sql="select * FROM users WHERE username='".$value."'";
  $query = mysql_query($sql) or die ("Could not match data because ".mysql_error());
  $num_rows = mysql_num_rows($query);

  if ($num_rows > 0) {
   echo "NO";
  } else {
   echo "YES";
  }
 }

I am basically trying to create a small registration form. If the username is already taken, I want to add the 'red' class, if not then 'green'.

The PHP here works fine, and returns either a "YES" or "NO" to determine whether it's ok.

The CSS:

input {

 border:1px solid #ccc;
}
.red {

 border:1px solid #c00;
}
.green {

 border:1px solid green;
 background:#afdfaf;
}

The Javascript I'm using is:

 $("#username").change(function() { 

             var value = $("#username").val();

  if(value!= '') {
   $.post("check.php", {
    value: value
   }, function(data){

   $("#test").html(data);

    if(data=='YES') {
     $("#username").removeClass('red').addClass('green');
    } if(data=='NO') {
     $("#username").removeClass('green').addClass('red');
    }
   });
  }  
 });

I've got the document.ready stuff too... It all works fine because the #test div html changes to either "YES" or "NO", apart from the last part where I check what the value of the data is. Here is the php:

 $value=$_POST['value'];

 if ($value!="") {
  $sql="select * FROM users WHERE username='".$value."'";
  $query = mysql_query($sql) or die ("Could not match data because ".mysql_error());
  $num_rows = mysql_num_rows($query);

  if ($num_rows > 0) {
   echo "NO";
  } else {
   echo "YES";
  }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-09-01 18:19:32

试试这个


if(data=='YES') 
  {
     $("#username").attr('style','').attr('style', 'border:1px solid green;background:#afdfaf;');
  } 
else
  {
     $("#username").attr('style','').attr('style', 'border:1px solid #c00;');
  }

让我知道它是否有效。

try this one


if(data=='YES') 
  {
     $("#username").attr('style','').attr('style', 'border:1px solid green;background:#afdfaf;');
  } 
else
  {
     $("#username").attr('style','').attr('style', 'border:1px solid #c00;');
  }

let me know if it works.

绅刃 2024-09-01 18:19:32

@Jimmy - 班级变化不会发生。

@Gaurav - 使用您的代码,边框会发生变化,但它始终设置为红色,无论数据是“是”还是“否”。

应该是查数据的事吧?但我不明白如何...

@Jimmy - The class change just doesn't happen.

@Gaurav - Using your code, the border changes but it's always set to red, whether the data is "YES" or "NO".

It must be something with checking the data? But I don't see how...

月寒剑心 2024-09-01 18:19:32

这是我认为您应该如何实现您的功能的基本示例。该代码几乎是不言自明的,但如果有任何不清楚的地方,请随时询问。

<?php
function readArray( $arr, $key, $default ='' ) { return isset($arr[$key]) ? $arr[$key] : $default ; }
function error( $message ){ echo "{ result: false, message: \"$message\" }"; exit; }
function success( $isFound ){ echo "{ result: true, exists: " . ($isFound?'true':'false') . "}" ; exit; }

if( sizeof( $_POST ) ) {
    $value = readArray( $_POST, "value", false ) ;
    if( false === $value ) error( "Empty username" ) ;

    @$db = mysql_connect("localhost","root","") ;
    if( !$db ) error( "Can't connect to database" ) ;

    @$result = mysql_select_db( "mysql" ) ;
    if( !$result ) error( "Can't select database" ) ;

    $query = "SELECT COUNT(User) as count FROM user WHERE User='" . mysql_real_escape_string( $value ) . "'";
    @$result = mysql_query( $query ) ;
    if( !$result ) error( "Can't execute query: " . mysql_error() ) ;

    success( readArray( mysql_fetch_assoc( $result ), "count" ) > 0 ) ;
}
?>
<script src="jquery.js" />
<style><![CDATA[
input {
    border:1px solid #ccc;
}
input.red {
    border:1px solid #c00;
}
input.green {
    border:1px solid #0c0;
}
]]></style>

<script>
$(document).ready(function(){
    var updateStyle = function() {
        var value = $("#username").val() ;
        if( !value ) return ;
        $.post("", {
            value: value
        }, function(data){
            var obj = eval( "(" + data + ")" ) ;
            if( obj.result ) {
                if( obj.exists ) {
                    $("#username").addClass('red');
                    $("#test").html( "User already exists" ) ;
                } else {
                    $("#username").addClass('green');
                    $("#test").html( "Username available" ) ;
                }
            } else {
                $("#test").html( obj.message ) ;
            }
        });
    }

    var timeout ;
    $("#username").keydown(function() {
        $("#test").html("") ;
        $("#username").attr("class", "") ;
        clearTimeout( timeout ) ;
        timeout = setTimeout( updateStyle, 500 ) ;
    });
});
</script>

<input id="username" name="username" value="" />
<div id="test"></div>

Here's a basic example of how I think you should implement your feature. The code is pretty much self-explanatory, but feel free to ask if anything's unclear.

<?php
function readArray( $arr, $key, $default ='' ) { return isset($arr[$key]) ? $arr[$key] : $default ; }
function error( $message ){ echo "{ result: false, message: \"$message\" }"; exit; }
function success( $isFound ){ echo "{ result: true, exists: " . ($isFound?'true':'false') . "}" ; exit; }

if( sizeof( $_POST ) ) {
    $value = readArray( $_POST, "value", false ) ;
    if( false === $value ) error( "Empty username" ) ;

    @$db = mysql_connect("localhost","root","") ;
    if( !$db ) error( "Can't connect to database" ) ;

    @$result = mysql_select_db( "mysql" ) ;
    if( !$result ) error( "Can't select database" ) ;

    $query = "SELECT COUNT(User) as count FROM user WHERE User='" . mysql_real_escape_string( $value ) . "'";
    @$result = mysql_query( $query ) ;
    if( !$result ) error( "Can't execute query: " . mysql_error() ) ;

    success( readArray( mysql_fetch_assoc( $result ), "count" ) > 0 ) ;
}
?>
<script src="jquery.js" />
<style><![CDATA[
input {
    border:1px solid #ccc;
}
input.red {
    border:1px solid #c00;
}
input.green {
    border:1px solid #0c0;
}
]]></style>

<script>
$(document).ready(function(){
    var updateStyle = function() {
        var value = $("#username").val() ;
        if( !value ) return ;
        $.post("", {
            value: value
        }, function(data){
            var obj = eval( "(" + data + ")" ) ;
            if( obj.result ) {
                if( obj.exists ) {
                    $("#username").addClass('red');
                    $("#test").html( "User already exists" ) ;
                } else {
                    $("#username").addClass('green');
                    $("#test").html( "Username available" ) ;
                }
            } else {
                $("#test").html( obj.message ) ;
            }
        });
    }

    var timeout ;
    $("#username").keydown(function() {
        $("#test").html("") ;
        $("#username").attr("class", "") ;
        clearTimeout( timeout ) ;
        timeout = setTimeout( updateStyle, 500 ) ;
    });
});
</script>

<input id="username" name="username" value="" />
<div id="test"></div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文