admin管理员组文章数量:1325108
I have a function which gets the data from various input ( multiple values ) and populate into a table.
Here is the jquery snippet :
var row =1
$("input[name='product_id[]']").each(function() {
$rows[row].cells[0].innerText = $(this).val();
row = row+1;
});
row=1;
$("input[name='product_name[]']").each(function() {
$rows[row].cells[1].innerText = $(this).val();
row = row+1;
});
row=1;
$("input[name='manufacturer[]']").each(function() {
$rows[row].cells[2].innerText = $(this).val();
row = row+1;
});
row=1;
$("input[name='position[]']").each(function() {
$rows[row].cells[3].innerText = $(this).val();
row = row+1;
});
I was wondering if I can bine multiple iterators into a single iterator ?
Thanks Kiran
I have a function which gets the data from various input ( multiple values ) and populate into a table.
Here is the jquery snippet :
var row =1
$("input[name='product_id[]']").each(function() {
$rows[row].cells[0].innerText = $(this).val();
row = row+1;
});
row=1;
$("input[name='product_name[]']").each(function() {
$rows[row].cells[1].innerText = $(this).val();
row = row+1;
});
row=1;
$("input[name='manufacturer[]']").each(function() {
$rows[row].cells[2].innerText = $(this).val();
row = row+1;
});
row=1;
$("input[name='position[]']").each(function() {
$rows[row].cells[3].innerText = $(this).val();
row = row+1;
});
I was wondering if I can bine multiple iterators into a single iterator ?
Thanks Kiran
Share Improve this question edited Mar 6, 2019 at 6:14 Kiran asked Mar 6, 2019 at 6:02 KiranKiran 8,54838 gold badges116 silver badges181 bronze badges4 Answers
Reset to default 4You can join the selectors by mas:
$("input[name='product_id[]'], input[name='product_name[]'], input[name='manufacturer[]'], input[name='position[]']")
.each(function() {
// ...
});
To be more DRY, use an array:
const selectorStr = ['product_id', 'product_name', 'manufacturer', 'position']
.map(str => `input[name='${str}[]']`)
.join(',');
$(selectorStr).each(function() {
// ...
});
If you need row
to be 1
in all but the first iteration, then:
['product_id', 'product_name', 'manufacturer', 'position']
.forEach((str, i) => {
if (i !== 0) {
row = 1;
}
$(`input[name='${str}[]']`).each(function(){
// etc
});
});
Create a mon function, this will help your row logic, which gets value 1 before each iteration
function iteratorOperation(){
}
And then pass this to the iterators,
$("input[name='product_id[]']").each(iteratorOperation);
row=1;
$("input[name='product_name[]']").each(iteratorOperation);
row=1;
$("input[name='manufacturer[]']").each(iteratorOperation);
row=1;
$("input[name='position[]']").each(iteratorOperation);
Use ,
to separate multiple selectors
$("input[name='product_id[]'],input[name='product_name[]'],input[name='manufacturer[],input[name='position[]']").each(function() {
});
Perhaps this is interesting?
note the escaping of the []
const fieldNames = ["product_id[]", "product_name[]", "manufacturer[]", "position[]"];
const selector = fieldNames.map(item => `input[name='${item}\\\\[\\\\]']`).join(", ")
$rows.each((row) => {
$(selector).each(() => {
let idx = fieldNames.indexOf(this.name)
row.cells[idx].innerText = $(this).val(); // jquery object or collection?
})
})
本文标签: javascriptPassing Multiple arguments to jQuery FunctionStack Overflow
版权声明:本文标题:javascript - Passing Multiple arguments to jQuery Function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126452a2421965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论