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 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高
  • ¥15 single positional indexer is out-of-bounds
  • ¥15 LSTM 模型数据量需要多少?
  • ¥15 mysql数据抓包开发报表
  • ¥15 linux系统下安装office打不开
  • ¥20 rsync脚本分发错误,文件不齐全
  • ¥15 labview顺序结构与时间延时
  • ¥15 脚本求解,源于数据库脚本变量筛选。