English 中文(简体)
how to tell if a property exists and is false
原标题:

I am having a hard time determining if data passed into the jquery template exists and is false without getting errors. This is what I am using to test

<html>
<head>
<title>jQuery Templates {{if}} logic</title>
</head>
<body>

<p id="results"></p>
<p>How do you test if the Value exists and is false?</p>

<script id="testTemplate" type="text/html">

    Test ${Test}:

    {{if Value}}
        Value exists and is true
    {{else}}
        Value doesn t exist or is false
    {{/if}}

    <br/>

</script>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.tmpl.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#testTemplate").tmpl({Test:1}).appendTo("#results");
        $("#testTemplate").tmpl({Test:2, Value:true}).appendTo("#results");
        $("#testTemplate").tmpl({Test:3, Value:false}).appendTo("#results");
    });
</script>

</body></html>

Does anyone know how to do it?

最佳回答

You can use another else statement in there using a === false check, like this:

{{if Value}}
    Value exists and is true
{{else typeof(Value) != "undefined" && Value === false}}
    Value exists and is false
{{else}}
    Value doesn t exist or isn t explicitly false
{{/if}}

You can test it out here. The typeof check is because you ll get a Value is not defined error with only Value === false. You would add other checks as well, for example {{else typeof(Value) == "undefined"}} would be true if the value wasn t specified.

问题回答

You could write a function to check for you:

$(document).ready(function() {
    function isExplicitlyFalse(f) { return f === false; }

    $("#testTemplate").tmpl({Test:1, isExplicitlyFalse: isExplicitlyFalse}).appendTo("#results");
    $("#testTemplate").tmpl({Test:2, Value:true, isExplicitlyFalse: isExplicitlyFalse}).appendTo("#results");
    $("#testTemplate").tmpl({Test:3, Value:false, isExplicitlyFalse: isExplicitlyFalse}).appendTo("#results");
});

then in your template:

{{if item.isExplicitlyFalse(Value)}}

Thow it this question is a bit old here is anoter possible solution. You can also use the Logical AND;

E.g:

if ( value && !value ) {
 // if value is defined and equals false
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签