doufei3561 2015-03-19 12:28
浏览 30
已采纳

在PHP中使用jquery addclass和removeclass

I' working on a PHP-Jquery-Ajax submit info form and I would like to use the addclass and removeclass jquery methods but I can't make it work as I want, so my question is: How do I have to fix the code below in order to add a css depending on the user input?

The addclass and removeclass are in the function verificaForm where I validate the input fields, in this case I only show you two fields validations but there are more... I also know that out there is a library(validate.js) that helps to validate the inputs from users but in this specific case I must have to sitck to this code.

here is my js code and thanks in advance:

$(document).ready(function () {

    $('#ID_formulario').on('submit', function (e) {

        e.preventDefault();

        var nombre = $('input#ID_nombre').val().trim();
        var email = $('input#ID_email').val().trim();

        if (validaForm(nombre, email)) {

            $('#result').html("<b>resolviendo peticion...</b>");

            var url = $(this).attr('action');
            var data = $(this).serializeArray();
            var type = $(this).attr('method');

            //...more code goes here ... it works fine ...
        }

    });
});

function validaForm(nombre, email) {

    if ((nombre == '') || nombre.replace(/s+/, '') == '') {
        alert("Favor especificar nombre");
        nombre.addClass('hightlight');
        return false;
    } else {
        else nombre.removeClass('hightlight');
    }

    if (nombre.length < 4) {
        alert('El valor del campo es muy corto');
        return false;
    }
    if ((email == '') || email.replace(/s+/, '') == '') {
        alert("Favor especificar correo");
        return false;
    }
    return true;
}
  • 写回答

2条回答 默认 最新

  • dongluojiao6322 2015-03-19 12:38
    关注

    You should pass the element to the function, not the value. Then You can obtain the value within the function. Something like that:

     var nombre = $('input#ID_nombre');
     var email = $('input#ID_email');
    
        if(validaForm(nombre, email)) 
    
        ....
    
    
       function validaForm(nombre,email){
    
       var nombre_value = nombre.val().trim();
       var email_value = email.val().trim();
       .......
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料