使用 java 脚本选择器迭代字典

发布于 2024-12-18 18:03:28 字数 1474 浏览 1 评论 0原文

我认为有一本字典,有 [输入ID,工具提示]。

另外,我在此页面中还有一组输入元素的 id。 我需要用我的元素 id 迭代字典:

 @{
 Dictionary<string, string> questionMarks = 
     ViewBag.QuestionMarks as Dictionary<string, string>;
 @}

 <script type="java-script">
 $(document).ready(function () {
    var inputs = $('input,select[type!="hidden"]');
    $.each(inputs, function(i, val) {
    if ($(val).attr('id')) { 
                var id = $(val).attr('id');

                var iter_string = '@questionMarks' + "[" + id + "]";
                alert(iter_string);              // [1]
                alert('@questionMarks["cvc"]');  // here i got tooltip
             }
    });
</script>

[1] 我有 System.Collection.Generic.Dictionary`2[System.String, System.String][cvc]

感谢 Jan Jongboom, 终于我得到了我想要的:

@using System.Web.Script.Serialization
@{
    Dictionary<string, string> questionMarks = ViewBag.QuestionMarks as Dictionary<string, string>;
    JavaScriptSerializer jss = new JavaScriptSerializer();
@}
<script type="text/javascript">
    var questionMarks = @Html.Raw(jss.Serialize((Dictionary<string, string>)ViewBag.QuestionMarks));     
    $(document).ready(function () {
        for ( keyVar in questionMarks ) {
            $('#' + keyVar).attr('original-title', questionMarks[keyVar]);
            $('#' + keyVar).tipsy({ gravity: 'w' });
        }
    });
</script>

I have a Dictionary in my view, there are
[inputID, tooltip].

Also i have a set of id's of my input elements in this page.
I need to iterate through the dictionary with my elements ids:

 @{
 Dictionary<string, string> questionMarks = 
     ViewBag.QuestionMarks as Dictionary<string, string>;
 @}

 <script type="java-script">
 $(document).ready(function () {
    var inputs = $('input,select[type!="hidden"]');
    $.each(inputs, function(i, val) {
    if ($(val).attr('id')) { 
                var id = $(val).attr('id');

                var iter_string = '@questionMarks' + "[" + id + "]";
                alert(iter_string);              // [1]
                alert('@questionMarks["cvc"]');  // here i got tooltip
             }
    });
</script>

[1] i have System.Collection.Generic.Dictionary`2[System.String, System.String][cvc]

Thanks to Jan Jongboom,
Finally i got that i wanted:

@using System.Web.Script.Serialization
@{
    Dictionary<string, string> questionMarks = ViewBag.QuestionMarks as Dictionary<string, string>;
    JavaScriptSerializer jss = new JavaScriptSerializer();
@}
<script type="text/javascript">
    var questionMarks = @Html.Raw(jss.Serialize((Dictionary<string, string>)ViewBag.QuestionMarks));     
    $(document).ready(function () {
        for ( keyVar in questionMarks ) {
            $('#' + keyVar).attr('original-title', questionMarks[keyVar]);
            $('#' + keyVar).tipsy({ gravity: 'w' });
        }
    });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初心未许 2024-12-25 18:03:28

执行类似的操作

<script>
var questionMarks = @new JavaScriptSerializer().Serialize((Dictionary<string, string>)ViewBag.QuestionMarks) ;
</script>

现在您有一个名为 questionMarks 的 javascript 变量,您可以对其进行迭代。

Do something like

<script>
var questionMarks = @new JavaScriptSerializer().Serialize((Dictionary<string, string>)ViewBag.QuestionMarks) ;
</script>

Now you have a javascript variable called questionMarks that you can iterate over.

笑饮青盏花 2024-12-25 18:03:28

不,您不能通过服务器变量从客户端代码进行迭代。您可以从 JS 变量(如 JSON)生成初始化代码或在服务器端生成必要​​的 html。您还可以通过 ajax 请求从 jsavascript 请求此类数据。

No, you can't iterate from the client side code through server variable. You can generate initialization code from JS variable (like JSON) or generate necessary html on the server side. Also you can requests for such data on from jsavascript via ajax request.

燃情 2024-12-25 18:03:28

在绘制输入时将数据属性附加到输入应该可以帮助您解决这一问题。

示例: http://jsfiddle.net/Krch9/

$(document).ready(function(e) {
    var inputs = $("input", "#container");
    $.each(inputs, function(i, val) {
        var ToolTipText = $(this).data('tooltiptext');
        /// Run your tooltip plugin here ...
        $("#output").html($("#output").html() + "<br>" + $(this).attr('id') + " : " + ToolTipText);
    });
});

使用 MVC 帮助程序,您可以轻松添加它们:

@Html.TextBoxFor(x => x.Field, new { data_tooltiptext = "Blah blah"});

标记将最终看起来像这样:

<input type="text" id="Field" data-tooltiptext="Blah blah">

编辑:额外帮助

I.E 循环遍历你的元素

foreach( var question in questionMarks) {
   @Html.TextBox(question.Key, new { data_tooltiptext = question.Value} );
}

然后使用 javascript 示例(你需要根据你的需要修改它......)在元素上运行插件

Attatching data attributes to the inputs as they are drawn should help you with this one.

Example: http://jsfiddle.net/Krch9/

$(document).ready(function(e) {
    var inputs = $("input", "#container");
    $.each(inputs, function(i, val) {
        var ToolTipText = $(this).data('tooltiptext');
        /// Run your tooltip plugin here ...
        $("#output").html($("#output").html() + "<br>" + $(this).attr('id') + " : " + ToolTipText);
    });
});

Using MVC helpers you can easily add them:

@Html.TextBoxFor(x => x.Field, new { data_tooltiptext = "Blah blah"});

The markup would end up looking something like this:

<input type="text" id="Field" data-tooltiptext="Blah blah">

Edit: Extra help

I.E Loop through your elements

foreach( var question in questionMarks) {
   @Html.TextBox(question.Key, new { data_tooltiptext = question.Value} );
}

Then use the javascript example (Youll need to modify it to your needs ...) to run the plugin on the elements

憧憬巴黎街头的黎明 2024-12-25 18:03:28

要删除引号,请使用 Html.Raw()

var questionMarks = '@Html.Raw(jss.Serialize((Dictionary<string, string>)ViewBag.QuestionMarks))';

To remove the quotes, use Html.Raw()

var questionMarks = '@Html.Raw(jss.Serialize((Dictionary<string, string>)ViewBag.QuestionMarks))';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文