2013-06-21 39 views
0

我嘗試過「.change」和「.live change」,但沒有成功。下面的代碼是我從谷歌示例請求中複製的許多代碼之一。我相信我需要使用「實時」更改,因爲在我的代碼中,我動態地創建了輸入字段。我無法讓jquery'change'工作,我做錯了什麼?

<!DOCTYPE html> 
<html> 
<head> 
    <title>test change</title> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
</head> 
<body> 
    <input id='inurlprodcgs' type="url" name="urlprodcgs" autocomplete="off" required/> 
</body> 
    <script> 
     if (typeof jQuery != 'undefined') { 
      alert("jQuery library is loaded!"); 
     }else{ 
      alert("jQuery library is not found!"); 
     } 
     $("input[type='url']").live('change', function(e) { 
      alert("hide"); 
     }); 
    </script> 
</html> 

正如你可以看到我已經測試,看看jQuery是加載。

+0

沒有你在你的瀏覽器控制檯? –

回答

4

,因爲現場被刪除(如您正在使用1.10) 使用.on()

$(document).on('change',"input[type='url']", function(e) { 
      alert("hide"); 
}); 
-2
<!DOCTYPE html> 
<html> 
<head> 
    <title>test change</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
</head> 
<body> 
    <input id='inurlprodcgs' type="url" name="urlprodcgs" autocomplete="off" required/> 
</body> 
    <script> 
     if (typeof jQuery != 'undefined') { 
      alert("jQuery library is loaded!"); 
     }else{ 
      alert("jQuery library is not found!"); 
     } 
     $("input[type='url']").change(function(e) { 
      alert("hide"); 
     }); 
    </script> 
</html> 
+0

小心解釋這是如何解決問題的?沒有上下文的代碼塊並不是真正的答案。 – David

+0

我使用了相同的提問者代碼。只是複製和粘貼,所有的工作 –

+0

除了提問者說「我動態創建輸入字段」的部分。您的解決方案不使用事件委託(原始代碼的作用,或者至少試圖這樣做),並且僅適用於最初加載時屬於DOM一部分的輸入字段。 – David

相關問題