2009-09-17 22 views
0

我在使用jQuery提交表單時遇到了一些問題。過去我已經提交了很多表單,但我只是在想,如何使用事件處理程序.submit()來提交表單並且它的元素沒有在表單ID中調用。問題是我似乎無法用$(this)鏈接元素(可能使用.children(),但我只想通過輸入字段使用.each())。使用.submit()事件處理程序通過JQuery循環表單域

下面的代碼片段:

$('.editTimeLink').click(function() { 
    var id = $(this).attr('rel'); 
    $.get('<?php echo $config["httpRoot"]; ?>/ajax.php?ajax=1&sec=time&a=edit&id=' + id, {}, function (data) { 
     if (data.returnCode == 1) { 
      $('#timeBox_' + id).html(data.data); 
      $('#timeBox_' + id + ' form').bind('submit', function() { 
       //$(this).$(':input').each(function() { 
       //$(this).(':input').each(function() { 
       $(this).each(':input', function() { 
        alert("adsf"); 
       }); 

       return false; 
      }); 
     } else if (data.returnCode == 0) { 
      alert(data.data); 
     } 
    }, 'json'); 

    return false; 
}); 

就像你所看到的,我想提醒的形式「這個」每個輸入元素字符串「ASDF」。

您可以看到兩行代碼在哪裏註釋掉了我一直試圖管理的內容。沒有被註釋掉的行也不起作用。我知道如何解決這個問題,例如將表單選擇器名稱傳遞給lambda函數,但我只是想,如果有更「乾淨」的方式來做到這一點?

在此先感謝。 Kristinn。

回答

1

爲什麼不能使用children()?您仍然可以使用each()

$(this).children(':input').each(...); 

然而,這不起作用:

$(this).each(':input', function() { 
    alert("test"); 
}); 

因爲each()只需要一個參數,回調(doc here)。順便說一下:使用一個JS調試器,例如Firebug,是一個好主意,找出爲什麼東西不工作。

+0

謝謝您的回答。是的,我確實知道這一點,但很高興完成這一切。非常感謝。 – 2009-09-17 15:58:00

2

。孩子()選擇直接孩子,所以如果你有一個表或其他標記在表單中你要使用.find()

$(this).find(':input').each(function(i){ 
    console.log($(this).val()); //$(this) now contains the current form field in the loop 
}); 
相關問題