2010-08-23 91 views
0

即時工作域檢查腳本它工作正常,當我在keyup上調用ajax,但默認情況下下拉有.com如果用戶選擇一個已經採取的域,我怎麼能得到這個腳本做另一個檢查時,用戶從.com切換到.net或.org?用下拉菜單調用jquery ajax()?

<!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

事情是這樣的:

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

這當然會複製你的代碼,所以我會建議它包裹裏面的函數。

+0

刪除「」 TLD左右,所以它應該是$(「選擇[名稱= TLD]」)... – Manie 2010-08-23 06:41:23

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> 
+0

您需要使用活。更多關於這裏http://api.jquery.com/live/ 這樣的事情:$(selecter).live('change',function(){...}); 或者第二個選項是類似的,但你必須自己綁定事件。 – realshadow 2010-08-23 07:53:32

+0

我不得不在明天之前完成。非常感謝!! .... – bruce 2010-08-23 08:19:50