0

我有一个注册表单,它会动态检查数据库中现有的用户名和电子邮件地址。但是,它似乎不适用于 FireFox 或 IE。不过,它在 Safari 和 Chrome 上很棒。这是代码:

<input type="text" name="username" id="username" placeholder="Username" class="registerinvitel" onKeyUp="resettooltip();" onFocusOut="check_register_exist('username');" autocomplete="off" />

和 JavaScript:

function check_register_exist(meth){
    var cueajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        cueajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            cueajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                cueajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    cueajaxRequest.onreadystatechange = function(){
        if(cueajaxRequest.readyState == 4){
var cuerez = cueajaxRequest.responseText;

if ( cuerez == 'exist' ) { infotooltip(meth, cuerez); 

document.getElementById('chk'+meth).value = "exist";

} else { document.getElementById('chk'+meth).value = "ok"; }


        }
    }


    var curchk = document.getElementById(meth).value;
    var queryString = "meth="+meth+"&u=" + curchk;
    cuenocache = Math.random();
    cueajaxRequest.open("GET", "/ajax/check.php?" + queryString + "&nocache=" + cuenocache, true);
    cueajaxRequest.send(null); 


}

有什么想法可以改变它以使其工作吗?AJAX check.php 代码如下(但我认为这不是问题):

<?

include  "../conf/config.php";

if(isset($_GET['meth'])) { $meth = $_GET['meth']; } else { $meth = ''; }
if(isset($_GET['u'])) { $u = $_GET['u']; } else { $u = ''; }


if ( $meth == 'username' ) {


$query_chkuser = mysql_query("SELECT * FROM members WHERE Username = '$u'");
$query_nr_res = mysql_num_rows($query_chkuser);

if ( $query_nr_res == '1' ) { echo "exist"; } else { echo "ok"; }


} 

elseif ( $meth == 'email' ) {


$query_chkuser = mysql_query("SELECT * FROM members WHERE Email = '$u'");
$query_nr_res = mysql_num_rows($query_chkuser);

if ( $query_nr_res == '1' ) { echo "exist"; } else { echo "ok"; }


} 

else {}
4

1 回答 1

7

尝试onBlur代替onFocusOut.

编辑:或者,甚至更好,onChange- 毕竟,如果用户没有更改文本,您不需要重新检查。

于 2013-05-17T17:38:45.600 回答