简体   繁体   English

ie9使用javascript检查是否为空

[英]ie9 check if empty using javascript

I am having trouble with the ie9 required attribute. 我在使用ie9 required属性时遇到了麻烦。 The attribute does work on ie10 and above but I will also want to get it worked at ie9. 该属性确实可以在ie10及更高版本上工作,但我也想在ie9上使其工作。

I have tried this: 我已经试过了:

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

This function should run when you click the submit-button(check on id) and then he will search for the id message and looks if it is empthy or not. 当您单击“提交”按钮(检查ID)时,该功能应运行,然后他将搜索ID消息并查看其是否为空。

But this does unfortunately not work for me in ie9. 但是不幸的是,这在ie9中对我不起作用。 Is there another solution that can solve this porblem? 还有另一种解决此问题的方法吗?

Solved it, here is the answer (I'm using laravel) 解决了,这是答案(我正在使用laravel)

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

Now all the errors will be listed, so if one or more input(s) are empty the alert will show it. 现在将列出所有错误,因此如果一个或多个输入为空,警报将显示它。

If you use a library like Modernizr which has a detection technique for html5 feature implementation but it doesn't have a browser detection capability. 如果您使用像Modernizr这样的库,该库具有用于html5功能实现的检测技术,但它不具有浏览器检测功能。

A solution is: 一个解决方案是:

You could try hooking in a simple detection script like this and then using it to make your choice. 您可以尝试挂钩这样的简单检测脚本,然后使用它来做出选择。 I've included Version Detection as well just in case that's needed. 我也包括了版本检测,以防万一。 If you only want to check of any version of IE you could just look for the navigator.userAgent having a value of "MSIE". 如果只想检查IE的任何版本,则只需查找值为“ MSIE”的navigator.userAgent。

Taken from here. 从这里拍摄。

But it's good to use $.trim() method before any value check for inputs. 但是在对输入进行任何值检查之前,最好使用$.trim()方法。

 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>"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM