[英]jquery optimal method of selecting in loop
我正在編寫一個從表單收集輸入的函數。 但是有很多選擇輸入的方法。 我環顧四周,無法根據以下方法找到具體答案。
是否有可能像在jquery選擇器中一樣將多個選擇添加到find()(以防我需要收集輸入和下拉列表)?
哪個最佳,為什么?
還有其他選擇嗎?
jQuery的
//method 1: Id selector
var formId = $('.modal.fade.in').find('form').attr('id');
$('#' +formId+ ' input').each(function(input)
{
//gather values
}
//method 2: Object and find
var form = $('.modal.fade.in').find('form');
$(form).find('input').each(function(input)
{
//gather values
}
只需創建一個更大的選擇器:
$('#' +formId+ ' input, #' + formId + ' select').each(function(element)
{
//gather values from inputs and selects
}
我會做類似的事情:
var v = {};
$('#' +formId).find('select, input, textarea'). each(function (){
var tmp;
if($(this).attr('type') == 'checkbox' || $(this).attr('radio') == 'checkbox')
{
tmp = $(this).is(':checked')?1:0;
}elseif($(this).attr('type') == 'submit'){
return;
}else{
tmp = $(this).val();
}
v[$(this).attr('id')] = tmp;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.