如何在输入密钥上停止表单提交?

时间:2015-03-30 06:17:35

标签: form-submit double-submit-prevention

我不想提交表格。
当我在文本框中按Enter键时,我的表单将被提交并自动重新加载。

如何停止提交表单?

我的代码是。

<form class="cart" id="cart-frm" style="margin-bottom: 10px; ">
    <div class="row">
        <div class="quantity col-md-6 col-sm-6 col-xs-6" style="width:auto;" >
            <label style="float: left; margin-top: 10px" for="quantity">Quantity : </label> 
            <span>&nbsp;</span>
            <input style="float: right;" type="text" class="input-text qty" id='qty' title="Qty" value="1" name="quantity" min="1" step="1">

        </div>
        <div class="quantity col-md-6 col-sm-6 col-xs-6 pull-right">                            
            <button type="button" style="float: right;"   id ='<?php echo $product['id']; ?>' class="btn btn-primary btn-icon add-to-cart-product"> Add to cart</button>
        </div><br>    
    </div>
    <span class="clearfix"></span>
</form>

1 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
    document.getElementById("cart-frm").onsubmit = function(){
        // if needs to be submited - return true
        // else - return false
        return false;
    };
</script>