Javascript或jquery检查表行值

时间:2017-09-22 07:16:15

标签: javascript jquery

我有一些javascript代码可以动态地将产品添加到表中。

但是,我需要一个nippet来检查是否已经存在包含产品的行以防止同一产品的多个条目?

我必须检查id =" Articles_0__ArticleId"和value =" 2",看看是否还有其他Articles_X__ArticleId也是2。

X是行号,基于0。但是,如果有人添加3个产品,我们会排0,1,2。如果你删除第1行,我们最终得到0,2。所以它必须考虑到这一点。

行如下所示:

<tr id="Articles[0]">
<td class="col-md-1">
    <input id="Articles_0__CollectionName" name="Articles[0].CollectionName" value="Articles" type="hidden">
    <input data-val="true" data-val-number="The field Artikelid must be a number." data-val-required="The Artikelid field is required." id="Articles_0__ArticleId" value="2" name="Articles[0].ArticleId" type="hidden">
    <input data-val="true" data-val-length="The field Bild must be a string with a maximum length of 50." data-val-length-max="50" id="Articles_0__ImagePath" name="Articles[0].ImagePath" value="/images/projector.jpg" type="hidden">
    <input data-val="true" data-val-required="The Namn field is required." id="Articles_0__Name" name="Articles[0].Name" value="Projektor" type="hidden">
    <input data-val="true" data-val-number="The field Pris must be a number." data-val-required="The Pris field is required." id="Articles_0__Price" name="Articles[0].Price" value="60,00" type="hidden">
    <input data-val="true" data-val-number="The field Order Base Id must be a number." data-val-required="The Order Base Id field is required." id="Articles_0__OrderBaseId" name="Articles[0].OrderBaseId" value="2" type="hidden">
    <input data-val="true" data-val-number="The field Totalpris must be a number." id="Articles_0__Total" name="Articles[0].Total" value="" type="hidden">
    <input data-val="true" data-val-number="The field Existing Quantity must be a number." id="Articles_0__ExistingQuantity" name="Articles[0].ExistingQuantity" value="" type="hidden">
    <input data-val="true" data-val-required="The Passed Production Time field is required." id="Articles_0__PassedProductionTime" name="Articles[0].PassedProductionTime" value="False" type="hidden">

    <input name="Articles.Index" value="0" type="hidden">

    <div class="image-thumbnail-normal">
        <img src="/images/projector.jpg" alt="article image">
    </div>
</td>
<td class="col-md-3">Projektor    </td>

<td class="col-md-1">
    60,00 kr / Styck
</td>
<td class="col-md-1">
    0,00 kr / Styck
</td>
<td class="col-md-1">
    <div class="form-group"><div class="col-sm-12 col-lg-13">
</td>
</tr>

2 个答案:

答案 0 :(得分:0)

我假设要检查的输入始终是其td中的第二个...这样的东西?

function alreadyExists(myId,myValue) {

    $('tr').each(function() {

        var $myInput = $(this).children('td').first().children('input').eq(1);

        if (($myInput.attr('id') == myId) && ($myInput.val() == myValue))
            return 1; // This id and value already exist
    });

    return 0;
}

如果可以的话,我会为这个输入添加一个类,所以你可以独立于它的位置选择它......

var $myInput = $(this).find('input.checkinginput');

我希望它有所帮助

答案 1 :(得分:0)

function already_exists?(new_val) {
    $('tr').each(function() {
        var $element = $(this).find("input[id$='ArticleId']");
        if ($element.val() == new_val)){
            return true;
        }
    });
    return false;
}