如何在操作onclick review.save之前验证必填字段

时间:2016-04-07 08:00:31

标签: javascript jquery html

如何在onclick="review.save();"

之前验证必填字段

在此操作onclick="review.save();之前是否有任何javascript要验证必填字段?

这是我的按钮代码

<button type="submit" title="<?php echo $this->__('Place Order') ?>" class="button btn-checkout" onclick="review.save();"><span><span><?php echo $this->__('Place Order') ?></span></span></button>

这是选择框代码:

<label class="commentlabel required-entry" for="ordercomment-comment">
                <?php echo $survey_question; ?> <em>*</em></label>
                <div class="input-box required-entry">
<select class="validate-select" id="socialtype" name="socialtype1" title="<?php echo $survey_question ?>">
<option value=""><?php echo $this->__("- Select -");?></option>
<option value="Friend/Relative/Acquaintance"><?php echo $this->__("Friend/Relative/Acquaintance");?></option>
<option value="Google search"><?php echo $this->__("Google search");?></option>
<option value="Gumtree Advert"><?php echo $this->__("Gumtree Advert");?></option>
<option value="Facebook"><?php echo $this->__("Facebook");?></option>
<option value="Other"><?php echo $this->__("Other");?></option>
</select>

这是opcheckout.js

var Review = Class.create();
Review.prototype = {
initialize: function(saveUrl, successUrl, agreementsForm){
    this.saveUrl = saveUrl;
    this.successUrl = successUrl;
    this.agreementsForm = agreementsForm;
    this.onSave = this.nextStep.bindAsEventListener(this);
    this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
},

save: function(){
    if (checkout.loadWaiting!=false) return;
    checkout.setLoadWaiting('review');
    var params = Form.serialize(payment.form);
    if (this.agreementsForm) {
        params += '&'+Form.serialize(this.agreementsForm);
    }
    params.save = true;
    var request = new Ajax.Request(
        this.saveUrl,
        {
            method:'post',
            parameters:params,
            onComplete: this.onComplete,
            onSuccess: this.onSave,
            onFailure: checkout.ajaxFailure.bind(checkout)
        }
    );
},

resetLoadWaiting: function(transport){
    checkout.setLoadWaiting(false, this.isSuccess);
},

1 个答案:

答案 0 :(得分:0)

您可以创建另一个验证字段的函数,如果成功则执行save函数。这是一个最小/ sudo代码示例

JS

var validation = {
   validate_review : function(){
      // do validation
      // ...
      if(success){
         review.save();
      }else{
         // show error message
      }
   }
}

HTML

<button type="submit" onclick="validation.validate_review ();" title="<?php echo $this->__('Place Order') ?>" class="button btn-checkout"><span><span><?php echo $this->__('Place Order') ?></span></span></button>