2016-07-01 29 views
0

這是我的例子:無法驗證後AJAX加載內容量

$.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: "fname=John&lname=Due", 
    success: function(result){ 
     $('#content').html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">'); 
    } 
}); 

和JS驗證數量

$(".quantity").keydown(function (e) { 
     // Allow: backspace, delete, tab, escape, enter and . 
     if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 || 
      // Allow: Ctrl+A, Command+A 
      (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) || 
      // Allow: home, end, left, right, down, up 
      (e.keyCode >= 35 && e.keyCode <= 40)) { 
      // let it happen, don't do anything 
      return; 
     } 
     // Ensure that it is a number and stop the keypress 
     if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { 
      e.preventDefault(); 
     } 
    }); 
+1

因爲你'.quantity'動態加載,你需要改變'$( 「量 」)。的keydown(功能(E){''到$(「 #內容」)上(KEYDOWN ,「.quantity」,function(e){' – Sean

+0

正如我在我的解釋中所解釋的,在綁定新事件之前,您已經解除綁定當前事件。https://jsfiddle.net/q52m0sks/ – asankasri

+0

查看結果取消綁定https://jsfiddle.net/q52m0sks/1/(點擊動態項目並查看提醒) – asankasri

回答

1

.html()是一個函數,其中的參數將被設置爲新html是在調用時傳遞,除非使用.html(function(index, html){})模式,其中在函數回調中新編htmlreturn;沒有設置爲屬性值爲.innerHTML是。替補

$('#content').html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">'); 

$('#content').html = 'Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">'; 

使用事件代表團附加​​事件動態創建.quantity元素;或創建

元素時附上事件
function handleKeydown(e) { 
     // Allow: backspace, delete, tab, escape, enter and . 
     if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 || 
      // Allow: Ctrl+A, Command+A 
      (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) || 
      // Allow: home, end, left, right, down, up 
      (e.keyCode >= 35 && e.keyCode <= 40)) { 
      // let it happen, don't do anything 
      return; 
     } 
     // Ensure that it is a number and stop the keypress 
     if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { 
      e.preventDefault(); 
     } 
    } 

$('#content') 
.html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">') 
.find(".quantity") 
.keydown(handleKeydown); 
0

html的是一個方法,你無法指定所以按照這樣的參考http://api.jquery.com/html/

$('#content').html('Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">'); 
1

正如其他人所說,你必須使用HTML()功能正常。順便說一句,如果你曾經動態地創建html內容,那麼你也必須綁定這些事件。

$.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: "fname=John&lname=Due", 
    success: function(result){ 
     $('#content').html = 'Label 1: <input name="quantity[]" class="quantity"> Label 2: <input name="quantity[]" class="quantity">'; 

     $(".quantity").unbind("keydown").bind("keydown", function() { 
      // your validation code should goes here 
     }); 
    } 
}); 
+0

供您參考:http://api.jquery.com/bind/ – asankasri

+0

示例:https:// jsfiddle .NET/q52m0sks / – asankasri