简体   繁体   English

如何将剃刀变量传递给js

[英]how to pass razor variable to js

I'm using MVC, I would like to pass a row index (razor variable) to a JS. 我正在使用MVC,我想将行索引(剃刀变量)传递给JS。

 @for (int i = 0; i < Model.Topics.Count(); i++)
                {
                    <tr id="row-@i"> 

                        <td class="IsReadOnly">@Html.TextBoxFor(model => model.Topics[i].NumberOfNoDifficulltySet, new { @readonly = "readonly", @class = "SubjectTB", @onchange = "CheckNoDiff(this);" })</td>

                    </tr>
                }

the js function is CheckNoDiff(this) - I would like to pass the @i parameter - that is the row index as an argument to the js. js函数是CheckNoDiff(this)-我想传递@i参数-这是将行索引作为js的参数。 How do I do that? 我怎么做? and how do I receive the parameter value on js? 以及如何在js上接收参数值?

Any help is most welcome - Regards, Lior. 任何帮助都是最欢迎的-问候,说谎者。

If you can use JQuery.data() then you can just add an attribute to your text box like so: 如果可以使用JQuery.data(),则可以将属性添加到文本框中,如下所示:

data-assigned-id="@i"

Then remove the parameter from the CheckNoDiff function and within the CheckNoDiff function you can access the value like so: 然后从CheckNoDiff函数中删除该参数,然后在CheckNoDiff函数中可以像这样访问该值:

var id= $(this).data('assigned-id');

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

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