使用相同的ID验证多个输入

时间:2013-10-18 10:47:25

标签: javascript jquery ajax

我正在我的一家magento商店开发一个“按SKU订购”系统。这是SKU和Qty所需的简单输入。

我在其中添加了“添加另一个sku”选项,其中包含以下功能:

function adicionarCampos(){
    var str = $('tabs-wrapper-cod').insert( '<div class="enc-cod-tab"><span class="enc_cod_02_home">Código: <input type="text" name="cod[]" value="" class="form_carrinho_cod required-entry" /></span><span class="enc_cod_04_home">Qt: <input type="text" name="qt[]" value="" class="form_carrinho_qt required-entry validate-number" /></span></div>' );
}

此功能为用户添加另一个“选项卡”以输入另一个SKU。这是参考的主要文件:

<div id="enc-cod-conteudo" title="Encomendar por código">
    <div class="enc-por-codigo">
        <p class="sub-enc-cod">Nunca foi tão simples encomendar por código!</p>
        <div class="quick-order">
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
                <div id="tabs-wrapper-cod">
                    <div class="enc-cod-tab"><span class="enc_cod_02_home">Código: <input type="text" name="cod[]" value="" class="form_carrinho_cod required-entry" /></span><span class="enc_cod_04_home">Qt: <input type="text" name="qt[]" value="" class="form_carrinho_qt required-entry validate-number" /></span></div
                </div>
        </div>
        <div class="actions_carrinho">
            <div class="adicionar-cod" onclick="adicionarCampos();"><span>Adicionar código</span></div>
            <button style="padding-left: 6px;" type="submit" title="Adicionar" class="button btn-update"><span><span>Adicionar ao carrinho</span></span></button>
        </div>
        </form>
    </div>
</div>
</div>

如您所见,添加的输入将具有相同的ID,“sku”。这是验证脚本正在寻找的内容,您可以在此处看到:

jQuery(document).ready(function(){
jQuery('#sku').keyup(sku_check);
});

function sku_check(){   
var sku = jQuery('#sku').val();
if(sku == "" || sku.length < 7){
jQuery('#sku').css('border', '1px #CCC solid');
jQuery('#tick').hide();
jQuery('#nome_do_produto').hide();
} else {
jQuery.ajax({
   type: "POST",
   url: "scripts/verificar_cod.php",
   data: 'sku='+ sku,
   cache: false,
   success: function(response){
if(response.length > 0){
    console.log(response);
    jQuery('#sku').css('border', '1px #090 solid');
    jQuery('#tick').fadeIn();
    jQuery('#cross').hide();
    jQuery('#nome_do_produto').html(response);
    jQuery('#nome_do_produto').fadeIn();
    jQuery('#codigo_inexistente').hide();
    } else {
    jQuery('#sku').css('border', '1px #C33 solid');
    jQuery('#cross').fadeIn();
    jQuery('#tick').hide();
    jQuery('#nome_do_produto').hide();
    jQuery('#codigo_inexistente').fadeIn();
         }
    }
    });
}
}

现在,我知道这不是正确的方法(ID本质上应该是唯一的),但javascript不是我的“海滩”可以这么说而且我坚持我将如何给添加输入唯一ID,此外,验证脚本必须查找所有这些递增的ID。

我的问题是,解决这个问题的最佳方法是什么?我应该通过JavaScript查看增量吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:1)

你是对的 - ID必须是唯一的!向它们添加一个类,例如'sku-element',然后使用该类获取所有元素:

$(".sku-element") //the result set
//or to go over the full result set and work with each item:
$(".sku-element").each(function(){
    $(this) //each of the selected items
});

此处不需要ID(您不应该为样式添加它们 - 使用类)。如果你必须有ID,你可以简单地创建一个变量来保存当前的元素数量,并在添加元素时动态更改它的id。

相关问题