[英]JavaScript One line problem
This code works: 此代码有效:
$('[id$=lbl]').val() == "0" ? alert('Bla') : null;
but this codes don't: 但是此代码不能:
$('[id$=lbl]').val() == "0" ? function(){alert('Bla'); return false; } : null;
$('[id$=lbl]').val() == "0" ? alert('Bla');return false; : null;
$('[id$=lbl]').val() == "0" ? return false : null;
i need "alert" after "return false" 我需要在“返回假”之后进行“提示”
How can I do that ? 我怎样才能做到这一点 ?
don't know exactly what you're trying to do (and you missed to show the error-message to us - but why don't you simply use a "normal" if-statement like this (which has no effect on performace but is much more readable (my opinion) and easier to change (if you have to, in the future)): 不知道您到底要做什么(而您却错过了向我们显示错误消息的原因-但是您为什么不简单地使用这样的“正常” if语句(这对性能没有影响,但是更具可读性(我认为)并且更容易更改(如果将来必须更改):
if($('[id$=lbl]').val() == "0"){
// do something
}else{
// do something else
}
you have to do like this: 您必须这样做:
$('[id$=lbl]').val() == "0" ?
(function(){alert('Bla'); return false; }
)() : null;
function(){alert('Bla'); return false; }
function(){alert('Bla'); return false; }
is just declaration of the function. function(){alert('Bla'); return false; }
只是函数的声明。 Brackets will execute it: (declaration of function...)()
方括号将执行它:( 函数声明...)()
EDIT:
编辑:
do you mean that you need to do like this?: 您是说需要这样做吗?:
function bla(){ return ($('[id$=lbl]').val() == "0" ? (function(){alert('Bla'); return false; })() : (function(){return null;})() ); }
explanation:
说明:
the function will return whatever ( condition ? val1 : val2 ) expression will return... in this case
val1 and
val2 are both functions and they are returning some values(val1 -> false; val2 -> null;) and that value is returned by main(bla) function.
函数将返回任何内容(条件?val1:val2)表达式将返回...在这种情况下,
val1和
val2都是函数,并且它们都返回一些值(val1-> false; val2-> null;),并返回该值通过main(bla)函数。
Do this: 做这个:
var test = $('[id$=lbl]').val() == "0" ? false : null;
if (test === false) {
alert('Bla');
return false;
}
You can't return from within a ?: construct. 您不能从?:构造中返回。 Even if you could it would be a very confusing piece of code to read.
即使您能够阅读,也将是一段非常混乱的代码。 Code is read far more often than it is written.
读取代码的频率远远超过编写代码的频率。
Better still if you're prepared to drop the ?: construct entirely use the answer from @oezi. 如果您准备删除?:更好,则完全使用@oezi的答案。
What you are doing is not practical: 您正在执行的操作不切实际:
$('[id$=lbl]').val() == "0" ? alert('Bla');return false; : null;
You can simply check its value like this: 您可以像这样简单地检查其值:
alert($('[id$=lbl]').val());
To go real-world/practical, you should actually be doing: 要进入现实/实用的环境,您实际上应该在做:
var myvalue = $('[id$=lbl]').val() == "0" ? when true : when false;
Example: 例:
var myvalue = $('[id$=lbl]').val() == "0" ? '' : $('[id$=lbl]').val();
Now myvalue
will either be empty when it contains 0
or will have the value of the field specified. 现在,当
myvalue
包含0
时将为空,或者具有指定字段的值。 You can change off course according to your requirement. 您可以根据需要更改路线。
$('[id$=lbl]').val() === 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.