admin管理员组

文章数量:1325408

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 badges
Add a ment  | 

4 Answers 4

Reset to default 4

You 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