2013-12-08 17 views
0

我不能得到這個工作,有什麼不對?模糊/焦點腳本有什麼問題?

我想設置值,在模糊和專注於我與類「ginput_quantity」輸入

<html> 
<head> 
<script src="jquery-1.10.2.min.js"></script> 
<script> 
$('.ginput_quantity').val(0); 

$(document).on('blur','.ginput_quantity',function(){ 
if (this.value == '') {this.value = '0';} 
}) 

$(document).on('focus','.ginput_quantity',function(){ 
if (this.value == '0') {this.value = '';} 
}) 
</script> 
</head> 
<body> 
<input type="text" value="" class="ginput_quantity"> 
<input type="text" value="" class="ginput_quantity"> 
<input type="text" value="" class="ginput_quantity"> 
<input type="text" value="" class="ginput_quantity"> 
<input type="text" value="" class="ginput_quantity"> 
</body> 
</html> 

鏈接:http://webintas.dk/jstest

回答

0

您的代碼將很好地工作,除了第一行$('.ginput_quantity').val(0);因爲你在其他代碼塊上使用event-delegation。所以那些部分不需要在.ready()處理程序中。

或者

如果你想實現這個概念,而不使用任何JS,你可以去place holders。請看here以獲得更多關於它的知識。

將您的代碼包裝在.ready()處理程序中。

試試這個

$(document).ready(function(){ 

    $('.ginput_quantity').val(0); 

    $(document).on('blur','.ginput_quantity',function(){ 
    if (this.value == '') {this.value = '0';} 
    }) 

    $(document).on('focus','.ginput_quantity',function(){ 
    if (this.value == '0') {this.value = '';} 
    }) 

}); 
+0

我知道佔位符 - 很聰明,但不是在我的情況。 – pstidsen