2014-04-05 117 views
0

在我的網絡表單中,我想要有幾個文本字段,其值爲'請輸入日期','輸入您的年齡'等,當文本字段獲得焦點並且用戶開始輸入數據時,這些字段會消失。我試圖用Javascript/JQuery編寫這個功能,但由於某種原因,我的JavaScript代碼沒有執行。Javascript代碼不能執行?

在我的HTML文檔的頭,我已經包括了我所寫作以及JQuery的來源:

<head> 
    {% load staticfiles %} 
    <script src="{% static 'jquery-1.11.0.js' %}"></script> 
    <script src="{% static 'text_area.js' %}"></script> 
</head> 

我對其中包含的一個形式的代碼文件中進一步下跌我文本字段:

<form action="" method="post"> {% csrf_token %} 
    <td> <input id="expire_date" 
      name="expire_date" 
      type="text" 
      value="{{list_expire_date}}"/> 
    </td> 
... 
</form> 

這裏是text_area.js來源:

$('#expire_date').focusin(function(){ 
    if(this.value == '{{list_expire_date}}') 
    { 
     this.value = ''; 
     showCalendarControl(this); 
    } 
}).focusout(function(){ 
    if(this.value == '') 
    { 
     this.value = '{{list_expire_date}}'; 
     hideCalendarControl(this); 
    } 
}) 
+0

這是HTML5佔位符的用途!你把代碼包裝在文檔中準備好了嗎? – adeneo

回答

0

的「的focusIn」事件觸發時,內輸入元素獲得焦點: https://www.twilio.com/docs/api/rest/sending-messages

你想你的代碼更改爲:

$('#expire_date').on('focus', function(){ 
if(this.value == '{{list_expire_date}}') 
{ 
    this.value = ''; 
    showCalendarControl(this); 
} 
}).on('blur', function(){ 
if(this.value == '') 
{ 
    this.value = '{{list_expire_date}}'; 
    hideCalendarControl(this); 
} 
}) 

或者,如果你將有多個輸入,將「focusin」事件處理程序綁定到表單。

+0

我將如何將事件處理程序綁定到窗體? – user3361761

+0

與你正在做的綁定輸入相同:'$('form')。focusin()'。然而,我建議這樣的:'('form')。on('focus','input.classname',function(){})。on('blur','input.classname',function (){});'。 –