用下拉菜单调用jquery ajax()?

时间:2010-08-23 06:32:30

标签: jquery dns html-select

我正在使用域检查脚本,当我在keyup上调用ajax时,它工作正常但默认情况下,如果用户选择已经采用的域,则下拉列表具有.com,如何让此脚本在执行时再执行一次检查用户从.com切换到.net或.org?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type='text/JavaScript'>

$(document).ready(function () {

    var validateUsername = $('#validateUsername');
    $('#domain').keyup(function () {
        var domain = $('#domain').val();
        var tld = $('#tld').val();
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);
            validateUsername.removeClass('error').html('checking...');
            this.timer = setTimeout(function () {
                $.ajax({
                    url: 'DomainCheck.php',
                    data: 'domain=' + domain + '&tld=' + tld,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
                        validateUsername.html(j.msg);
                    }
                });
            }, 3500);
            this.lastValue = this.value;
       }
    });
});

</script>
</head>
<body>
    <form action="" method="post">
            <fieldset> 
                <legend>choose your domain</legend> 
                <div> 
                  <span class="httpFont">http://www.</span>
                  <input type="text" name="domain" value="" id="domain" autocomplete="off" />  
                  <select id="tld" name="tld">
                     <option selected="selected" value="com">.com</option>
                     <option value="net">.net</option>
                     <option value="org">.org</option>
                  </select>&nbsp;<span id="validateUsername"></span>
                </div> 
           </fieldset> 
           <input type="hidden" name="action" value="register" /> 
           <div class="submit">
           <input type="submit" alt="Submit button"> 
    </form> 
</body>
</html>

2 个答案:

答案 0 :(得分:2)

这样的事情:

$('select[name="tld"]').change(function() {
    //your ajax code
});

这当然会复制你的代码,所以我建议把它包装在一个函数中。

答案 1 :(得分:0)

感谢重播,我把它包装成一个函数,直到第二个请求

例如默认.com我改为.net它不起作用,我改为.org然后它开始工作。我也记录了所有签入MySQL及其创建重复条目

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type='text/JavaScript'>

$(document).ready(function () {

    var validateUsername = $('#validateUsername');
    $('#domain').keyup(function () {
        var domain = $('#domain').val();
        var tld = $('#tld').val();
        if (this.value != this.lastValue) {
            if (this.timer) clearTimeout(this.timer);
            validateUsername.removeClass('error').html('checking...');
            this.timer = setTimeout(function () {
                $.ajax({
                    url: 'DomainCheck.php',
                    data: 'domain=' + domain + '&tld=' + tld,
                    dataType: 'json',
                    type: 'post',
                    success: function (j) {
                        validateUsername.html(j.msg);
                    }
                });
            }, 3500);
            this.lastValue = this.value;
       }
    });
});

</script>
<script type="text/javascript">
function checkDom(){
var validateUsername = $('#validateUsername');
$('select[name="tld"]').change(function() {
var domain = $('input[name="domain"]').val();
var tld = $('select[name="tld"]').val();    
validateUsername.removeClass('error').html('checking...');
$.ajax({
url: 'DomainCheck.php',
data: 'domain=' + domain + '&tld=' + tld,
dataType: 'json',
type: 'post',
success: function (j) {
validateUsername.html(j.msg);
}
});     
});
}
}
</script>

</head>
<body>
    <form action="" method="post">
            <fieldset> 
                <legend>choose your domain</legend> 
                <div> 
                  <span class="httpFont">http://www.</span>
                  <input type="text" name="domain" value="" id="domain" autocomplete="off" />  
                  <select onchange="checkDom()" id="tld" name="tld">
                     <option selected="selected" value="com">.com</option>
                     <option value="net">.net</option>
                     <option value="org">.org</option>
                  </select>&nbsp;<span id="validateUsername"></span>
                </div> 
           </fieldset> 
           <input type="hidden" name="action" value="register" /> 
           <div class="submit">
           <input type="submit" alt="Submit button"> 
    </form> 
</body>
</html>