如果span id包含包含特定文本的php数据

First time posting but this script has got me completely stuck...

Ill explain a few things first

I need to create a click function on a register button that when clicked it checks the span id e.g $("#username_results) for php return data that has specific text e.g " Username in use.". Then do a function else do something else.

So here is the HTML part i have cropped out the non relevant parts

          <td width="168"><div align="right">Username :</div></td>
          <td width="144"><input name="username" type="text" id="username" form="registration"></td>
          <td width="162" height="16"><div align="left"><span class="error_text" id="username_results"></span></div></td>

and here is the JQuery

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

    $(this).val($(this).val().replace(/\s/g, ''));

    var username = $(this).val();

    if(username.length < 4){

        $("#username_results").css({"color":"red"});    
        $("#username_results").html('<img src="images/not_available.png" />' + " Invalid username.")
        }

    if(username.length >= 4) {

        $("#username_results").html('<img src="images/loader.gif" />');
        $.post('scripts/ajax_registration.php', {'username':username}, function(username_data) {
        $("#username_results").html(username_data);
        });
    }
});

and here is the PHP

include 'sql_connection.php';

if(isset($_POST["username"]))

{

if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !='xmlhttprequest') {
    die();
}

$username =  strtolower(trim($_POST["username"])); 

$username = filter_var($username, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);

$results = mysqli_query($sql_connection,"SELECT id FROM members WHERE name='$username'");

$username_exist = mysqli_num_rows($results);

if($username_exist) {
    echo ('<script>$("#username_results").css({"color":"red"})</script>');
    echo ('<img src="images/not_available.png" />');
    echo (" Username in use.");
}else{
    echo ('<script>$("#username_results").css({"color":"green"})</script>');
    echo ('<img src="images/available.png" />');
    echo (" Username available.");
}

mysqli_close($sql_connection);

}

so what i was trying was

$("#register").click(function(e) {

    var username_results = $("#username_results").val();

    if (username_results.indexOf("Username") > 0) {

    alert("YAY ITS WORKING")    

    }

});

and this was not returning any alerts unless i took the > 0 out then it would show the alert at all times even when nothing was displayed in that span.

Any help would be appreciated. Thank you

You need to use

$("#username_results").text()

<span> doesn't have value that's why it's returning nothing to you. I've made JSBin for you: http://jsbin.com/renivuroziqi/2/edit