简体   繁体   English

如何将Eval()传递给Javascript函数

[英]How to pass Eval () to Javascript function

This is probably really simple for someone who's familiar with Javascript, but I have having trouble calling a function while passing an eval as a parameter. 对于熟悉Javascript的人来说,这可能真的很简单,但是我在传递eval作为参数时调用函数时遇到了麻烦。 I've got a bunch of dynamic checkboxes that I'm trying to create. 我有很多尝试创建的动态复选框。 I've got the text displaying great, but the checkbox is giving me trouble. 我的文字显示很好,但是复选框给我带来了麻烦。 The problem is, the value that I'm passing to the checkbox is a string (either "true" or "false") not a bool. 问题是,我传递给复选框的值是一个字符串(“ true”或“ false”)而不是布尔值。

<asp:CheckBox runat="server" AutoPostBack="True" Checked='<%# Eval("Value") %>' Text='<%# Eval("DisplayName") %>'></asp:CheckBox>

I also created a short javascript function to convert it to boolean 我还创建了一个简短的javascript函数,将其转换为布尔值

function ConvertToBoolean(value)
{
     if (value == "true")
     {    return true;}
     if (value == "false")
     {    return false;}
 }

I'm not great with javascript, but I've tried everything I could find on SO. 我对javascript不太满意,但是我已经尝试了所有可以在SO上找到的东西。 Not sure why it's not working for me. 不知道为什么它对我不起作用。 Here's what I've tried: 这是我尝试过的:

Checked='<%# Eval("Value") %>'
Checked="'<%# Eval("Value") %>'"
Checked='<%# "ConvertToBoolean(" + Eval("Value") + ");" %>'
Checked='<%# "ConvertToBoolean(\"" + Eval("Value") + "\");" %>'
Checked='<%# Eval("Value", "javascript:ConvertToBoolean({0});") %>'
Checked="ConvertToBoolean('<%# Eval("Value") %>')"

Am I misunderstanding something fundamental here? 我在这里误解了一些基本的东西吗? Is there even a way to do this? 有没有办法做到这一点?

<asp:CheckBox runat="server" AutoPostBack="True" Checked='<%# Eval("Value")=="true" %>' Text='<%# Eval("DisplayName") %>' />

This code gets executed at server side and is translated to HTML, something like 此代码在服务器端执行,并转换为HTML,类似于

<input type="checkbox" checked="checked"> .....

Ofcourse ASP needs to evaluate your Eval("Value")=="true" to decide if it needs to output checked or not 当然,ASP需要评估您的Eval("Value")=="true"以确定是否需要输出checked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM