.each()函数影响当前对象

时间:2016-11-10 13:28:37

标签: javascript jquery html

我的代码遇到了一些问题,首先是HTML:

<table class="finance-table">
  <tbody><tr>
    <th></th>
    <th>Deposit</th>
    <th>Balance</th>
    <th>Fees</th>
    <th>Total Payable</th>
    <th>Term</th>
    <th>Fixed Rate</th>
    <th>Representative APR</th>

    <th>Monthly Pmt</th>

  </tr>
  <tr class="hp">
    <td><strong>HP</strong></td>
    <td id="td_finance_deposit">£11700.00</td>
    <td id="td_finance_balance">£105300.00</td>
    <td id="td_finance_fees">£298.00</td>
    <td id="td_finance_total_inc_deposit">£146255.50</td>
    <td id="td_finance_term">60 mths</td>
    <td id="td_finance_rate">5.50%</td>
    <td id="td_finance_apr">10.1%</td>

    <td id="td_finance_monthly_payments">£2242.59 p/m*  x 60 mths</td>

  </tr>
</tbody></table>

这些表中大约有10个[在同一个文档中],所有表格都具有相同的ID和类别。我使用每个循环来对每个找到的表执行一些代码,但是它似乎只在第一个表上工作而忽略了其他表。

下面是jQuery,就像我在第一张桌子上找到的作品一样,但忽略了其余部分!

<!-- Remove First and Final Payment from Showroom Finance Examples -->
<script>
  $(".finance-table").each(function(key, value) {
    // Display loading
    var html = $(this);

    // Remove the First Payment and Final Payment Column
    $(this).find("#td_finance_first_payment, #td_finance_final_payment").remove();
    $(this).find("th:contains('1st Pmt')").remove(); $(this).find("th:contains('Final Pmt')").remove();

    // Get the Term and update the monthly payment
    var term = $(this).find("#td_finance_term").html(); // .replace(/\D/g,'')
    var payments = ($(this).find("#td_finance_monthly_payments").html()).split('x')[0];
    ($(this).find("#td_finance_monthly_payments")).html(payments + " x " + term);
  })
</script>

编辑: 请注意,我无法更改HTML

2 个答案:

答案 0 :(得分:3)

您应该首先为每个<td>提供一个唯一的ID,可能还有该记录的数据库标识符。你现在不需要它,但如果你需要的话,这将允许你以后再做其他事情。

然后将所有<td> ID更改为类:

<td class="td_finance_fees">£298.00</td>

最后相应地更改所有的javascript以使用class而不是ID:

$(this).find(".td_finance_first_payment, .td_finance_final_payment").remove();

答案 1 :(得分:3)

使用Attribute Equals Selector

更改您的代码:

void Process()
{
    PdfReader reader = new PdfReader(mInFileName);
    // 1 type of fields
    AcroFields formFields = reader.AcroFields;
    int countAcroFields = formFields.Fields.Count;
    // 2 another type of fields
    PRAcroForm form = reader.AcroForm;
    List<PRAcroForm.FieldInformation> fieldList = form.Fields;
    // 3 i think this is the same as 2
    PdfDictionary acroForm = reader.Catalog.GetAsDict(PdfName.ACROFORM);
    PdfArray acroFields = (PdfArray)PdfReader.GetPdfObject(acroForm.Get(PdfName.FIELDS), acroForm);
    // 4 another type of fields
    XfaForm xfaForm = formFields.Xfa;
    bool flatten = false;

    if (countAcroFields > 0)
    {
        //No fields found
    }
    else if (fieldList.Count > 0)
    {
        //No fields found
    }
    else if (xfaForm.XfaPresent == true)
    {
        //No xfaForms found
        ReadXfa(reader);
    }
    else
    {
        // Yes, there are annotations and this code extracts them but does NOT flatten them
        PdfDictionary page = reader.GetPageN(1);
        PdfArray annotsArray = page.GetAsArray(PdfName.ANNOTS);
        if ( annotsArray == null)
            return;
        else
        {
            List<string> namedFieldToFlatten = new List<string>();
            foreach (var item in annotsArray)
            {
                var annot = (PdfDictionary)PdfReader.GetPdfObject(item);

                var type = PdfReader.GetPdfObject(annot.Get(PdfName.TYPE)).ToString(); //Expecting be /Annot
                var subtype = PdfReader.GetPdfObject(annot.Get(PdfName.SUBTYPE)).ToString(); //Expecting be /Widget
                var fieldType = PdfReader.GetPdfObject(annot.Get(PdfName.FT)).ToString(); //Expecting be /Tx

                if (annot.Get(PdfName.TYPE).Equals(PdfName.ANNOT) &&
                    annot.Get(PdfName.SUBTYPE).Equals(PdfName.WIDGET))
                {
                    if (annot.Get(PdfName.FT).Equals(PdfName.TX))
                    {
                        flatten = true;
                        var textLabel = PdfReader.GetPdfObject(annot.Get(PdfName.T)).ToString(); //Name of textbox field
                        namedFieldToFlatten.Add(textLabel);
                        var fieldValue = PdfReader.GetPdfObject(annot.Get(PdfName.V)).ToString(); //Value of textbox
                        Console.WriteLine($"Found Label={textLabel} Value={fieldValue}");
                    }
                }
            }

            if (flatten == true)
            {
                // Flatten the PDF [11/9/2016 15:10:06]
                string foldername = Path.GetDirectoryName(mInFileName);
                string basename = Path.GetFileNameWithoutExtension(mInFileName);
                string outName = $"{foldername}\\{basename}_flat.pdf";
                using (var fStream = new FileStream(outName, FileMode.Create))
                {
                    //This totally removes the fields instead of flattening them
                    var stamper = new PdfStamper(reader, fStream) { FormFlattening = true, FreeTextFlattening = true, AnnotationFlattening = true };
                    var stamperForm = stamper.AcroFields;
                    stamperForm.GenerateAppearances = true;
                    foreach (var item in namedFieldToFlatten)
                    {
                        stamper.PartialFormFlattening(item);
                    }
                    stamper.Close();
                    reader.Close();
                }
            }
        }
    }
}

为:

$(this).find("#td_finance_first_payment, #td_finance_final_payment").remove();

$(this).find('td[id="td_finance_first_payment"], td[id="td_finance_final_payment"]').remove(); #xxx

的所有区域进行此类更改

这样做是找到属性为id =“xxx”的所有tds,而不是使用#id标识符,这迫使jQuery进行树搜索。

此外,您的HTML与您的代码不符,(您的HTML中没有td_finance_first_payment,我假设您将其删除了吗?)

编辑:如果您100%无法编辑html(来自您无法控制的来源,例如API或内部软件),此解决方案非常有用。最好的解决方案是修复ID!