Understanding the Acquisition of Specific Elements and Attribute Values Using jQuery.each().f()

Asked 2 years ago, Updated 2 years ago, 43 views

Using jQuery, from HTML

"hoge1 hoge1_2 quantity: 1, hoge2 hoge2_2 quantity: 2, hoge3 hoge3_2 quantity: 3..." Repeat for class="list_" minutes in class="boxlist_"
I would like to pick up only certain attributes, concatenate strings, and return the return value.

Also, regarding the quantity,
in select I would like to get only those with selected elements as values.

▼ HTML

<table class="boxlist_">
<tbody>
    <tr class="list_">
        <td class="name_">
            <div class="name_">

                <div class="name1_"><a href="/hoge/"title="hpge">hoge1</a>>/div>;            
                <span class="name2_">hoge1_2</span>
            </div>
        </td>
        <td class="price_box_"nowrap="nowrap">
            <p class="qty_"> quantity <br>
            <select name="qty1"><option value="1">1</option value="2">2>/option value="3">3>/option>>>>4="4">"
            </select>
            </p>
        </td>
    </tr>
    <tr class="list_">
        <td class="name_">
            <div class="name_">
                <div class="name2_"><a href="/hoge/"title="hpge">hoge2</a>>/div>;            
                <span class="name2_">hoge2_2</span>
            </div>
        </td>
        <td class="price_box_"nowrap="nowrap">
            <p class="qty_"> quantity <br>
            <select name="qty1"><option value="1">1;/option value="2" selected="">2>/option value="3">3>/option value="3">3>/option>4>">vout;4>al"
            </select>
            </p>
        </td>
    </tr>
    <tr class="list_">
        <td class="name_">
            <div class="name_">
                <div class="name3_">a href="/hoge/"title="hpge">hoge3</a>>            
                <span class="name2_">hoge3_2</span>
            </div>
        </td>
        <td class="price_box_"nowrap="nowrap">
            <p class="qty_"> quantity <br>
            <select name="qty1"><option value="1">1</option value="2">2>/option value="3" selected="3<3</option>4>>>vout;4>=">4>vout.
            </select>
            </p>
        </td>
    </tr>
</tbody>
</table>

▼Trying js

function(){
    var result=[];
    jQuery(".boxist_tr.list_").map(function(index,Element){
    variableName=jQuery(Element).find('td.name_div.name_').text().replace(/\s+/g, ");
    varitemQty=jQuery(Element).find('p.qty_select option: selected').attr('value'); 
    result.push(itemName+"quantity:"+"+itemQty).join(","); // Add to Array
    return result;
    });
    // console.log(result);
}

In this case, the value is [ ] and
I can't get the desired value.

I would appreciate it if you could let me know how to correct it.
Thank you for your cooperation.

javascript html jquery

2022-09-30 16:44

1 Answers

The first mistake is

jQuery(".boxist_tr.list_").map(function(index,Element){

.boxist.boxlist.

result.push(itemName+"quantity:"+"+itemQty).join(","); // Add to Array

Since map(), there is no need to push() in the callback, and the timing of join() is strange.

function(){
  let result=jQuery(".boxist_tr.list_").map(function(index,element){
    letitemName=jQuery(element).find('td.name_div.name_').text().replace(/\s+/g, ");
    let itemQty=jQuery(element).find('p.qty_select option: selected').attr('value'); 
    return`${itemName} quantity:${itemQty}`;
  }).get().join(",");
  return result;
}


2022-09-30 16:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.