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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵