weixin_33712987 2019-06-02 11:31 采纳率: 0%
浏览 94

是否可以验证输入是否为空?

是否可以验证输入是否为空?

我想检查数据是否已从html获取值,如果有则应禁用按钮,如果没有就不禁用按钮。

这是示例html:

<!DOCTYPE html>
<html>
    <head>
        <title>Untitled Document</title>
    </head>

    <body>
        <form method="post">
            <label>email
                <input type="text" name="email" />
            </label>
        </form>
    </body>
</html>
<script>
  $(document).ready(function () {
    $("#Submit").click(function(event) {
       Execute();
    });

    function Execute(){
      $.ajax({
        type: 'POST',
        url: 'test.php',
        data: { 'email': $("input[name='email']").val() },
         success: function(res) {
         if (data) {
          $("#Submit").attr("disabled", true);
        $('#success').text(res.response);
    } if (!data) {
    $("#Submit").attr("disabled", false);
    $('#error').text(res.error_msg);
    } else {  // do nothing }
  },
        error: function(resp) {
          alert("failed");
        }
      });
    };

  });
</script>
  • 写回答

3条回答 默认 最新

  • weixin_33713350 2019-06-02 12:58
    关注

    U can validate the values before triggering an ajax request. As u can serialize your form data and then validate your required values for the request and if they validate then trigger the ajax call with required data

     function Execute(){
     // Contains all the inputs that are present in your form
     var formData = $('form').serializeArray();
    
     // Validate your values 
    
     // If values does not matches your requirements, return false with error like
    
     alert('All required values not filled');return false;
    
    
     // If code reaches here, means you have all your required values. 
     // So, making ajax request makes more sense now as it can be executed successfully as values are first validated then ajax is triggered
    
     $.ajax({
      // Your code for ajax request
    
     })
    
    }
    

    Perform value validation in the loop for any required values

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler