只想使用返回值并忽略警报
Only want to use the return value and ignore the alert
我想创建一个函数,它将使用下面粘贴的多个函数。我想忽略警报,只使用返回值,这可能吗?感谢
function ageFormatAll()
{
var age = document.getElementById('ageGrade').value;
var patt=/^(?:'d{2}'.'d{2}|)$/;
if (! patt.test(age) )
{
alert("Value entered doesn't match the correct format");
return "Age grade doesn't match format.'n";
} else
{ return "" };
}
当然不能。只需从功能中删除警报。
但是有一种垃圾和不可靠的方法可以实现你想要的:
// remember alert to restore later
var alert_ = alert;
// override alert with an empty function
alert = function() {};
// get the result
var result = ageFormatAll();
// restore the alert function
alert = alert_;
但不要这样使用:这是一件很好的事情,知道但不要使用。
是否删除警报?或者,如果要有条件地显示警报,请修改函数。将if检查作为参数。这样,您就可以在其他场景中原样重新使用它,并且只需在不需要警报时传递一个参数。
function ageFormatAll(doNotShowAlert)
{
var age = document.getElementById('ageGrade').value;
var patt=/^(?:'d{2}'.'d{2}|)$/;
if (! patt.test(age) )
{
if(doNotShowAlert !== undefined && doNotShowAlert) {
alert("Value entered doesn't match the correct format");
}
return "Age grade doesn't match format.'n";
} else
{ return "" };
}
相关文章:
- Javascript返回值只在循环中返回一次
- XMLHttpRequest未返回值-状态202
- 根据是否解析了 Promise 从函数返回值
- Angular,函数在(模型)工厂中返回值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何从客户端的数组中获取用户输入和返回值
- Javascript中带有返回值的嵌套函数
- 为变量分配多个nodejs导出返回值时出现问题
- Datatable get列值只给出前10行
- Javascript只返回键,不返回值
- 当我只想要1时,获取返回的所有错误消息
- 如何使用javascript从数据库中获取值,我使用的是jquery自动完成,我只想从数据库中搜索值
- 我无法让它作为 JavaScript 函数工作(我在控制台时得到我想要的返回值.log但我不能将其作为函数调用)
- 只想使用返回值并忽略警报
- Meteor:如何为集合中的每个值只返回一个条目
- 只想比较时间值MomentJS
- 我想从数据库加载事件日历,返回值失去效果和日期不显示
- 列表中最频繁出现的数字(模式)-只想获得最高的值
- 你如何解析一个数组删除键,只返回值与javascript
- 我想要每个单独的返回值从reduce()函数,而不是总数