使用javascript中的View处理布尔值 [英] working with bool value from View in javascript

查看:89
本文介绍了使用javascript中的View处理布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型中具有bool属性:

I have a bool property in a model:

public bool IsExistSchedule {get; set;}

在视图中,我写了以下内容:

In the View I wrote the following:

<div id="step1" data-schedule="@Model.IsExistSchedule">
...
</div>

在生成的页面中,data-schedule包含True值.
如何在javascript中正确使用此值?
例如,我要检查变量是否为真:

In the generated page data-schedule contain True value.
How to properly work with this value in javascript?
For example, I want to check if variable is true:

var isExist = $('#step1').attr('data-schedule');
if( isExist === "True")
     do something

我不喜欢这部分isExist === "True".我应该将其转换为bool可能是其他名称.恐怕在其他浏览器中,变量可能包含其他值:true, True, "True", "true".
谢谢.

I don't like this part isExist === "True". I should convert it to bool may be or something other. I'm afraid that in other browser the variable may contain other values: true, True, "True", "true".
Thanks.

推荐答案

通常,如果您需要使用javascript的任何模型,则最好在您的视图中执行此操作:

Generally if you need any of your model for javascript, it's a good idea to do this in you views:

@Json.Encode(Model.IsExistSchedule)

这会将值转换"为javascript可以理解的内容,例如将一个小写的布尔值括起来.

This will "convert" the value into something javascript will understand, like lower-casing a boolean.

然后,就像@Musa所建议的那样,使用.data()方法.

And then, like @Musa suggests, use the .data() method.

这篇关于使用javascript中的View处理布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆