javascript 将变量值后面的代码传递给 jQuery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12817546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:11:17  来源:igfitidea点击:

Pass code behind variable value to jQuery function

c#javascriptjqueryasp.net

提问by Crista23

I am not an expert in jQuery and I am trying to pass some variable values from C# to my function called on keyup and onclick events. So far I have something like this:

我不是 jQuery 专家,我试图将一些变量值从 C# 传递给我在 keyup 和 onclick 事件上调用的函数。到目前为止,我有这样的事情:

$('mydiv').bind('keyup click', function(event) {}

but what I need should be:

但我需要的应该是:

$('mydiv').bind('keyup click', function(event, UserId, ControlId) {}

, where UserId and ControlId are some ids I am getting in code behind from the query string. I am also using jQuery 1.6.4.

,其中 UserId 和 ControlId 是我在查询字符串后面的代码中获取的一些 ID。我也在使用 jQuery 1.6.4。

How can I do this, preferably without using hidden input fields? Thank you.

我该怎么做,最好不使用隐藏的输入字段?谢谢你。

回答by Johan

Use oninstead of bind

使用on代替bind

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.

从 jQuery 1.7 开始, .on() 方法是将事件处理程序附加到文档的首选方法。

Passing values from the server to the client with razor (if youre using asp.net mvc):

使用 razor 将值从服务器传递到客户端(如果您使用的是 asp.net mvc):

$('mydiv').on('keyup click', function(event, @UserId, @ControlId) {}

or if its webforms:

或者如果它的网络表单:

$('mydiv')
    .on('keyup click', function(event, <%= UserId %>, <%= ControllId %>) {}

回答by bekite

I would use data-attributes:

我会使用数据属性:

$('mydiv').data({ userId: <%= UserId %>, ControllId: <%= ControllId %> })

then you can access those data in the on click event:

然后您可以在点击事件中访问这些数据:

$('mydiv').on('click', function(event) {
  var userId = $(this).data('userId');
  var ControlId = $(this).data('ControlId');
});

回答by Neo

declare the variable as public in code behind

在后面的代码中将变量声明为 public

public string userId="abc"; 

Access it on client side

在客户端访问它

var uid='<%=userId %>';
$('mydiv').bind('keyup click', function(event, uid, ControlId) {}

回答by Sarthak Gupta

A js file cannot directly access C# objects so you need to do something like below. Even if you want to write complete jQuery code in your view file, you can still follow same approach.

js 文件不能直接访问 C# 对象,因此您需要执行如下操作。即使您想在视图文件中编写完整的 jQuery 代码,您仍然可以遵循相同的方法。

So you can pass variables in some Model which is passed to View and once you have those variables in Model you can do something like below:

因此,您可以在传递给 View 的某些 Model 中传递变量,一旦在 Model 中拥有这些变量,您就可以执行以下操作:

 <script type="text/javascript">
    var myList= @Html.Raw(Json.Encode(@Model.UsersList));      
</script>

So now you have a json object which can be accessed by any individual js file as well with in same view file with the help of variable "myList".

所以现在你有了一个 json 对象,它可以被任何单独的 js 文件访问,也可以在同一个视图文件中借助变量“myList”访问。

回答by MySqlError

Javascript scopes are not like scopes in other languages so if you write

Javascript 作用域与其他语言中的作用域不同,因此如果您编写

var UserId = 5; 
var ControlId = 5;

$('mydiv').bind('keyup click', function(event) 
{
  alert( UserId );  
});

it will work

它会起作用

check out http://jsfiddle.net/FgYTL/1/

查看http://jsfiddle.net/FgYTL/1/

回答by krg

Is my mydiv a class, id or a jQuery variable? Looks like you need to do

我的 mydiv 是一个类、id 还是一个 jQuery 变量?看起来你需要做

$('div.mydiv') or $('div#mydiv')