Magento自定义表单验证

时间:2016-09-22 10:00:58

标签: javascript jquery forms magento prototype

我有Magento版本1.7.0.2 Billing Checkout表单2输入类型="复选框"。

我的HTML for checkbox1:

<span class="field-row" id="checkbox1">

<div>
    <div><input type="checkbox" class="validate-checkboxgroup-required validation-failed"
                name="billing[amcustomerattr][checkbox1][]" id="checkbox1___385" value="385">&nbsp;Value1
    </div>
</div><div>
    <div><input type="checkbox"  name="billing[amcustomerattr][checkbox1][]" id="checkbox1___384" value="384">&nbsp;Value2
    </div>
</div><div>
    <div><input type="checkbox"  name="billing[amcustomerattr][checkbox1][]" id="checkbox1___383" value="383">&nbsp;Value3
    </div>
</div>
</span>

和Checkbox2:

<span class="field-row" id="checkbox2">

<div>
    <div><input type="checkbox" class="validate-checkboxgroup-required validation-failed"
                name="billing[amcustomerattr][checkbox2][]" id="checkbox2___385" value="385">&nbsp;Value4
    </div>
</div><div>
    <div><input type="checkbox"  name="billing[amcustomerattr][checkbox2][]" id="checkbox2___384" value="384">&nbsp;Value5
    </div>
</div><div>
    <div><input type="checkbox"  name="billing[amcustomerattr][checkbox2][]" id="checkbox2___383" value="383">&nbsp;Value6
    </div>
</div>
</span>

如何检查至少一个复选框(包含多个框),如何进行javascript表单验证。

我在提交Button上创建了一个jQuery事件以阻止提交:

    function nbOfCheckedBoxes(spanId) {
        var selected = [];
        jQuery("#"+spanId+" input:checked").each(function () {
            selected.push(jQuery(this).attr('name'));
        });
        return selected.length;
    }

jQuery( "#billing-buttons-container button" ).click(function(e) {
    if(nbOfCheckedBoxes('checkbox1')==0 && nbOfCheckedBoxes('checkbox2')==0)  {
        //alert( "NOT OK" );
        e.preventDefault();
        return false;
    }

    });

但它不起作用。有任何想法吗 ?

非常感谢

0 个答案:

没有答案