ie9使用javascript检查是否为空

时间:2015-12-17 09:15:20

标签: javascript jquery internet-explorer internet-explorer-9

我遇到了ie9必需属性的问题。该属性适用于ie10及以上,但我也想在ie9上使用它。

我试过这个:

$('#submit-button').click(function(){
  if($('#message').val() == ''){
     alert('Input can not be left blank');
  }
});

当您单击提交按钮(检查ID)时,此功能应该运行,然后他将搜索id消息并查看它是否为真。

但遗憾的是,这在ie9中对我不起作用。还有其他解决方案可以解决这个问题吗?

解决了,这是答案(我正在使用laravel)

    @if($errors->any())
    <br>
    <div class="alert alert-danger">
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </div>
@endif

现在将列出所有错误,因此如果一个或多个输入为空,则警报将显示该错误。

1 个答案:

答案 0 :(得分:1)

如果您使用像Modernizr这样的库,它具有html5功能实现的检测技术,但它没有浏览器检测功能。

解决方案是:

  

您可以尝试在这样的简单检测脚本中挂钩,然后使用它来做出选择。我还包括版本检测,以防万一。如果您只想检查任何版本的IE,您可以查找值为&#34; MSIE&#34;的navigator.userAgent。

Taken from here.

但在对输入进行任何值检查之前使用$.trim()方法是件好事。

&#13;
&#13;
var BrowserDetect = {
  init: function() {
    this.browser = this.searchString(this.dataBrowser) || "Other";
    this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "Unknown";
  },
  searchString: function(data) {
    for (var i = 0; i < data.length; i++) {
      var dataString = data[i].string;
      this.versionSearchString = data[i].subString;

      if (dataString.indexOf(data[i].subString) !== -1) {
        return data[i].identity;
      }
    }
  },
  searchVersion: function(dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index === -1) {
      return;
    }

    var rv = dataString.indexOf("rv:");
    if (this.versionSearchString === "Trident" && rv !== -1) {
      return parseFloat(dataString.substring(rv + 3));
    } else {
      return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    }
  },

  dataBrowser: [{
      string: navigator.userAgent,
      subString: "Edge",
      identity: "MS Edge"
    }, {
      string: navigator.userAgent,
      subString: "MSIE",
      identity: "Explorer"
    }, {
      string: navigator.userAgent,
      subString: "Trident",
      identity: "Explorer"
    }, {
      string: navigator.userAgent,
      subString: "Firefox",
      identity: "Firefox"
    }, {
      string: navigator.userAgent,
      subString: "Opera",
      identity: "Opera"
    }, {
      string: navigator.userAgent,
      subString: "OPR",
      identity: "Opera"
    },

    {
      string: navigator.userAgent,
      subString: "Chrome",
      identity: "Chrome"
    }, {
      string: navigator.userAgent,
      subString: "Safari",
      identity: "Safari"
    }
  ]
};

BrowserDetect.init();

$('#submit-button').click(function() {
  if (BrowserDetect.browser == "MSIE" && BrowserDetect.version < 10 && $.trim($('#message').val()) == '') {
    alert('Input can not be left blank');
    $('#message').focus();
  }
});

document.write("You are using <b>" + BrowserDetect.browser + "</b> with version <b>" + BrowserDetect.version + "</b>");
&#13;
&#13;
&#13;