使用 java 脚本选择器迭代字典
我认为有一本字典,有 [输入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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行类似的操作
现在您有一个名为
questionMarks
的 javascript 变量,您可以对其进行迭代。Do something like
Now you have a javascript variable called
questionMarks
that you can iterate over.不,您不能通过服务器变量从客户端代码进行迭代。您可以从 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.
在绘制输入时将数据属性附加到输入应该可以帮助您解决这一问题。
示例: http://jsfiddle.net/Krch9/
使用 MVC 帮助程序,您可以轻松添加它们:
标记将最终看起来像这样:
编辑:额外帮助
I.E 循环遍历你的元素
然后使用 javascript 示例(你需要根据你的需要修改它......)在元素上运行插件
Attatching data attributes to the inputs as they are drawn should help you with this one.
Example: http://jsfiddle.net/Krch9/
Using MVC helpers you can easily add them:
The markup would end up looking something like this:
Edit: Extra help
I.E Loop through your elements
Then use the javascript example (Youll need to modify it to your needs ...) to run the plugin on the elements
要删除引号,请使用 Html.Raw()
To remove the quotes, use Html.Raw()