gpt4 book ai didi

javascript - ie9使用javascript检查是否为空

转载 作者:行者123 更新时间:2023-11-27 23:33:32 27 4
gpt4 key购买 nike

我在使用 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

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

最佳答案

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

解决方案是:

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".

Taken from here.

但最好在对输入进行任何值检查之前使用 $.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>");

关于javascript - ie9使用javascript检查是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34330730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com