验证按下输入按钮时不起作用,但单击按钮时起作用

时间:2016-10-20 10:19:39

标签: javascript jquery

我的ajax电话

function cleanString<T>(path: T): T;
function cleanString(path) {
    if (typeof path === 'string') {
        return path.replace(/\\+/g, '/').replace(/\/+/g, '/').replace(/^\/+/g, '');
    }
    return path;
}

我的HTML代码

    $("#go_btn").on('click',function(){

                $.ajax({

                      url: '<c:url value="/filter/save" />',

                      data: $('input,select',"#requestForm").not(".hint").serialize(),
                      type: 'POST',
                      cache: false,
                      success: function(data){                       

                          if(data.success == true){
                              $('.error',"#requestForm").hide().removeData('errorsData');

                              $("#requestForm").submit();

                          }else if(data.success == false){
                              $('.error',"#requestForm").hide().removeData('errorsData');
                              var errors = data.errors;               
                              for (var key in errors) {                  
                                    var errorDesc = errors[key];
                                    var $question = $('input[name="'+key+'"],select[name="'+key+'"]',"#requestForm").parents('td');
                                    var $errorDiv = $('div.error',$question);
                                    var errorsData = $errorDiv.data('errorsData'); 

                                    if(!errorsData){
                                        errorsData = new Array();                             
                                        $errorDiv.data('errorsData',errorsData);                          
                                    }

                                    errorsData[errorsData.length] = errorDesc;

                                    var errorHTML = '';
                                    for(var i=0 ; i < errorsData.length;i++){
                                        if(i>0){
                                              errorHTML += '<br />';
                                        }
                                        errorHTML += errorsData[i]; 

                                    }

                                    $('.error_message',$errorDiv).html(errorHTML);
                                    $errorDiv.show();

                                    $(this).scrollTop(0);
                                    $('input[name="'+key+'"]').focus();
                                    $('select[name="'+key+'"]').focus();

                                }
                          }
                      }       
                });

控制器方法

 <a href="#" title="click next" id="go_btn" style="color:#fff;text-decoration:none;"> 

验证在用户单击按钮时有效,但在用户按下回车键时不起作用。如何将回车键功能绑定到我的代码以使其正常工作。

编辑:

我的表格

    @RequestMapping(value = "/filter/save", method = RequestMethod.POST)

@ResponseBody
public Map<String, Object> saveFilterDetails(
        @ModelAttribute(value = "bean") Bean bean,
        BindingResult result, Model model) {

    Map<String, Object> output = new HashMap<String, Object>();
    Map<String, String> fieldErrors = new HashMap<String, String>();

    if (bean.getIndustryId() == 0 || bean.getIndustryId() <= 0) {
        result.rejectValue("industryId", "bean.filter.industry.mandatory");
    }

    if (bean.getRoleId() == 0 || bean.getRoleId() <= 0) {
        result.rejectValue("roleId", "bean.filter.role.mandatory");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(result, "customerName",
            "bean.customername.mandatory");


    if (result.hasErrors()) {
        output.put("success", false);

        for (FieldError fieldError : result.getFieldErrors()) {
            try {
                String value = messageSource.getMessage(fieldError, null);
                fieldErrors.put(fieldError.getField(), value);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        output.put("errors", fieldErrors);
        return output;
    }
    else
    {   
        output.put("success", true);

        return output;
    }

}

1 个答案:

答案 0 :(得分:2)

替换<a> - 代码,然后插入<button type="submit">