javascript - 两个按钮中只有一个需要输入

标签 javascript php jquery html css

我有一个表单,其中有一个文本字段,该文本字段是我的两 (2) 个按钮中的一个 (1) 的必需条目。第一个 (1st) 按钮将文本字段中的代码应用于我商店购物车部分中的产品。第二个 (2nd) 从购物车部分的所有产品中删除所有代码。

解决这个问题的最佳方法是什么?

谢谢。

<div id="cart-coupon-menu" class="coupon-menu-hide">
<form id="discount-coupon-form" action="<?php echo $this->getUrl('checkout/cart/couponPost') ?>" method="post">
    <div class="discount">
        <div class="discount-form">
            <input type="hidden" name="remove" id="remove-coupone" value="0" />
            <div class="input-box">
                <input class="input-text" id="coupon_code" name="coupon_code" value="<?php echo $this->escapeHtml($this->getCouponCode()) ?>" placeholder="Enter a Coupon or Promo Code" autocomplete="off"/>
            </div>
            <div class="buttons-set">
                <button type="button" title="<?php echo $this->__('Apply Coupon') ?>" class="button" onclick="discountForm.submit(false)" value="<?php echo $this->__('Apply Coupon') ?>"><span><span><?php echo $this->__('Apply Coupon') ?></span></span></button>
                <button type="button" title="<?php echo $this->__('Cancel Coupon') ?>" class="button" onclick="discountForm.submit(true)" value="<?php echo $this->__('Cancel Coupon') ?>"><span><span><?php echo $this->__('Cancel Coupon') ?></span></span></button>
            </div>
        </div>
    </div>
</form>
</div>

上述表单将被序列化并通过 AJAX 设置到 Controller ,并返回适当的响应。

当输入框为空时,我希望输入框充当必填字段,并禁止通过第一个按钮提交。但是,当它为 null 时,第二个按钮仍应允许提交。当在输入框中输入文本时,它们都表现正常。

目前我正在尝试做这样的事情:

<script type="text/javascript">
    //<![CDATA[
    var discountForm = new VarienForm('discount-coupon-form');
    discountForm.submit = function (isRemove) {
        if (isRemove) {
            $('coupon_code').removeClassName('required-entry');
            $('remove-coupone').value = "1";
        } else {
            $('coupon_code').addClassName('required-entry');
            $('remove-coupone').value = "0";
        }
        if(something where I identify if it is required and the field is null){return null;}
        else{continue with ajax call;}

最佳答案

我认为这只是一个简单的 javascript,例如:

<button type="button" title="<?php echo $this->__('Apply Coupon') ?>" class="button" onclick="isValid() ? discountForm.submit(false) : handleInvalid()" value="<?php echo $this->__('Apply Coupon') ?>"><span><span><?php echo $this->__('Apply Coupon') ?></span></span></button>

我在那里使用了一个三元运算符...所以基本上它说如果 isValid() 返回 true,执行:discountForm.submit(false) 否则执行:handleInvalid().

那么 javascript 函数将是:

function isValid() {
    var couponCode = document.getElementById('coupon_code').value;

    return /* whatever logic you want here... */
}

function handleInvalid() {
    // do whatever you want to the coupon_code input to indicate it's required and pop up an error message
    alert("Please enter a coupon code!");
}

关于javascript - 两个按钮中只有一个需要输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726314/

相关文章:

javascript - ng-repeat 与 JSON 对象不打印

javascript - Twitter 的 Bootstrap 日期时间选择器

javascript - 使用 Fetch API 和 fs.createWriteStream 对文件进行流式响应

重命名 asp.net 页面后 JavaScript 不工作

php - Symfony2-显示错误的凭证错误

php - 如何避免在表单的某些文本字段中列出以前输入的值? HTML, PHP

php - jQuery : getting PHP variables by AJAX

javascript - Anchor Cycler/Dropdown 定期导入学校类(class)数据

jquery - 内联文本区域填充剩余宽度

javascript - jquery 在控制台中有效,但在链接的 js 文件中无效