2014-03-30 18 views
0

我使用的是O/S插件腳本不必硬編碼 - 重複html選擇元素的所有10個實例的塊?我可以設置一個隱藏的表單元素的索引值設置爲它並使用像這樣。()。val()?這有點超出了我的編碼技能。謝謝。如何凝結的jQuery代碼重複塊

$('#serviceTypeID\\[1\\],#serviceTypeID\\[2\\],#serviceTypeID\\[3\\],#serviceTypeID\\[4\\],#serviceTypeID\\[5\\],#serviceTypeID\\[6\\],#serviceTypeID\\[7\\],#serviceTypeID\\[8\\],#serviceTypeID\\[9\\],#serviceTypeID\\[10\\]').change(function() { 

     // 1st identical instance of the block 
     var first = parseInt($('#firstService\\[1\\]').val()); 
     var second = parseInt($('#secondService\\[1\\]').val()); 
     var third = parseInt($('#thirdService\\[1\\]').val()); 
     if (isNaN(first)) first = 0; 
     if (isNaN(second)) second = 0; 
     if (isNaN(third)) third = 0; 

     $('#serviceTotal\\[1\\]').val((first + second + third + ' Total')); 

     // 2nd identical instance of the block 
     first = parseInt($('#firstService\\[2\\]').val()); 
     second = parseInt($('#secondService\\[2\\]').val()); 
     third = parseInt($('#thirdService\\[2\\]').val()); 
     if (isNaN(first)) first = 0; 
     if (isNaN(second)) second = 0; 
     if (isNaN(third)) third = 0; 

     $('#serviceTotal\\[2\\]').val((first + second + third + ' Total')); 

     // 3rd identical instance of the block 
     first = parseInt($('#firstService\\[3\\]').val()); 
     second = parseInt($('#secondService\\[3\\]').val()); 
     third = parseInt($('#thirdService\\[3\\]').val()); 
     if (isNaN(first)) first = 0; 
     if (isNaN(second)) second = 0; 
     if (isNaN(third)) third = 0; 

     $('#serviceTotal\\[3\\]').val((first + second + third + ' Total')); 

    //and so on up to 10 currently 

     // 10th identical instance of the block 
     first = parseInt($('#firstService\\[10\\]').val()); 
     second = parseInt($('#secondService\\[10\\]').val()); 
     third = parseInt($('#thirdService\\[10\\]').val()); 
     if (isNaN(first)) first = 0; 
     if (isNaN(second)) second = 0; 
     if (isNaN(third)) third = 0; 

     $('#serviceTotal\\[10\\]').val((first + second + third + ' Total')); 

    }); 
+2

除了重構擺脫重複的,你真的應該使用基本參數與'parseInt':'變種X = parseInt函數(FOO,10);'。 –

+0

謝謝你的建議@IngoBürk這也超出了我目前的技能發展進程。 –

+0

在'parseInt'的每個調用中添加',10'都超出了你的技能範圍? –

回答

2

首先,你需要class屬性爲選擇,更容易和更簡單的選擇。

<select class="serviceType" name="serviceTypeID[1]" id="serviceTypeID[1]" ... 
<select class="serviceType" name="serviceTypeID[2]" id="serviceTypeID[2]" ... 

然後你就可以在函數調用中使用的循環:

$('.serviceType').change(function() { 

    for(var i=1; i<=10; i++){ 
     var first = parseInt($('#firstService\\['+ i +'\\]').val(), 10); 
     var second = parseInt($('#secondService\\['+ i +'\\]').val(), 10); 
     var third = parseInt($('#thirdService\\['+ i +'\\]').val(), 10); 
     if (isNaN(first)) first = 0; 
     if (isNaN(second)) second = 0; 
     if (isNaN(third)) third = 0; 

     $('#serviceTotal\\['+ i +'\\]').val((first + second + third + ' Total')); 
    } 
}); 
+0

這也是一個很好的建議。我會盡力將兩個答案都加入到我的解決方案中。謝謝@ogur –

+0

這工作完美。謝謝@ogur!正是我所需要的... –

+0

再次感謝@ogur。祝你有美好的一天! –

1

a for loop也許?

for (var i = 1; i <= 10; i++) { 
    $('#serviceTypeID\\[' + i + '\\]').change(function() { 

     var first = parseInt($('#firstService\\[' + i + '\\]').val(), 10); 
     var second = parseInt($('#secondService\\[' + i + '\\]').val(), 10); 
     var third = parseInt($('#thirdService\\[' + i + '\\]').val(), 10); 
     if (isNaN(first)) first = 0; 
     if (isNaN(second)) second = 0; 
     if (isNaN(third)) third = 0; 

     $('#serviceTotal\\[' + i + '\\]').val((first + second + third + ' Total')); 

    }); 
} 
+0

非常酷@naveen。感謝您的建議。我會立即嘗試併發回。所以非常感謝您的時間和幫助。 –

+0

這工作完美。謝謝@naven!正是我所需要的... –

+0

高興地幫助,我的朋友。 – naveen

2

你可以在你的選擇框使用下面的片段循環與你有當前的HTML。話雖如此,我想知道更多你的HTML結構(你的選擇的父母)將允許更簡單的解決方案。

// All select boxes. 
var selects=jQuery("select[name^='serviceTypeID']").change(function() { 
    // Pattern to find the counter. 
    var matcher = new RegExp("serviceTypeID\\[(\\d+)\\]"); 

    // Invoke your code for each select box. 
    jQuery.each(selects, function(index, select){ 
     // Get the name. 
     var name=jQuery(select).prop("name"); 
     // Extract the counter. 
     var number = matcher.exec(name)[1]; 

     // Your block. 
     var first = parseInt($('#firstService\\['+number+'\\]').val(), 10); 
     var second = parseInt($('#secondService\\['+number+'\\]').val(), 10); 
     var third = parseInt($('#thirdService\\['+number+'\\]').val(), 10); 
     if (isNaN(first)) first = 0; 
     if (isNaN(second)) second = 0; 
     if (isNaN(third)) third = 0; 

     $('#serviceTotal\\['+number+'\\]').val((first + second + third + ' Total')); 
    }); 
}); 
+0

好的,謝謝@AugustusKling。我實際上正在實施2個先前的激情,並讓我的腳本使用它們。但我非常感謝你的建議。 –

+0

更動態循環這種方式。好:) +1 – naveen

+0

所有3種解決方案對循環10個以上的元素盒組都是有效且正確的。我添加了該模式以允許其他組計數和省略組編號。選擇最適合您的案例的解決方案。 –