从数据属性将字符串解析为对象[重复]

2023-12-03

我在使用 jQuery 验证插件时遇到了很多麻烦,解决这个问题的唯一方法是使用.submitHandler属性并在其中做一些技巧。

其中检查触发器的父级是否是字段集以及是否有data-submit-handler属性它将执行我发送到那里的任何内容。

它看起来像这样:

<fieldset data-submit-handler="SITE.Products.QA.Bindings.post">

在这种情况下SITE.Products.QA.Bindings.post将是一个函数。问题是我不知道如何将数据属性中的字符串解析为对象而不是字符串,以便我可以执行我引用的函数。有办法做到吗?


您可以使用一个帮助程序来解决它:

// convert string representation of object in to object reference:
// @p: object path
// @r: root to begin with (default is window)
// if the object doesn't exist (e.g. a property isn't found along the way)
// the constant `undefined` is returned.
function getObj(p, r){
    var o = r || window;
    if (!p) return o;                      // short-circuit for empty parameter
    if(typeof p!=='string') p=p.toString();// must be string
    if(p.length==0) return o;              // must have something to parse
    var ps = p.replace(/\[(\w+)\]/g,'.$1') // handle arrays
              .replace(/^\./,'')           // remove empty elements
              .split('.');                 // get traverse list
    for (var i = 0; i < ps.length; i++){
        if (!(ps[i] in o)) return undefined; // short-circuit for bad key
        o = o[ps[i]];
    }
    return o;
}

// couple extensions to strings/objects

// Turns the string in to an object based on the path
// @this: the string you're calling the method from
// @r: (optional) root object to start traversing from
String.prototype.toObject = function(r){
    return getObj(this,r);
};

// Retrieves the supplied path base starting at the current object
// @this: the root object to start traversing from
// @s: the path to retrieve
Object.prototype.fromString = function(p){
    return getObj(p,this);
};

因此,示例用法:

window.foo = {
    bar: {
        baz: {
            hello: 'hello',
            world: function(){
                alert('world!');
            }
        }
    },
    list: [
        {item: 'a'},
        {item: 'b'},
        {item: 'c'}
    ]
};

var a = 'foo.bar.baz.hello';
console.log(getObj(a));    // hello
console.log(a.toObject()); // hello

var b = 'foo.bar.baz.world';
console.log(getObj(b)());  // world

var c = 'baz.hello';
console.log(getObj(c, foo.bar)); // hello
console.log(foo.bar.fromString(c)); // hello
console.log(c.toObject(foo.bar)); // hello

var d = 'foo.list[1].item';
console.log(getObj(d)); // b
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从数据属性将字符串解析为对象[重复] 的相关文章

随机推荐

  • SQL Bigquery:将特定组的选择限制为 10

    下面是示例表 目前 该表对于每个 ID 都有无限的条目 我的要求是 首先按 rand 升序对 ID 进行排序 然后只取前 2 行 ID 和 companies CREATE TABLE table name ID int companies
  • 将 Jetty 绑定到 IPv6 地址

    我正在尝试将 Jetty 绑定为仅侦听 IPv6 地址 我正在使用 Jetty 7 4 2 v20110526 我的jetty xml
  • 在图像上绘制对角线

    您好 我正在尝试在图像的右上角到左下角之间绘制对角线 这是到目前为止我的代码 width getWidth picture height getHeight picture for x in range 0 width for y in r
  • 根据月份日期向数据表添加季节列

    我正在使用 data table 我正在尝试创建一个名为 season 的新列 它基于名为 MonthName 的列创建一个具有相应季节的列 例如夏季 冬季 我想知道是否有更有效的方法来根据月份值将季节列添加到数据表中 这是 300 000
  • 默认情况下是否启用可选依赖项?

    如果我定义一个依赖项 例如foo version 1 0 0 optional true 当我执行 cargo run 时它会可用吗 我可以检查它是否在代码中启用吗 if cfg feature foo 似乎不起作用 就像该功能一直缺失一样
  • matplotlib 条形图中的极限误差线

    我试图让误差条显示在置信区间的限制处 而不是显示在中心 我想要的是这样的 但我得到的是这样的 为了绘制条形图 我使用了这个 import pandas as pd import numpy as np import matplotlib p
  • 验证视图状态 MAC 失败错误

    尝试通过传递参数来运行报表查看器 但收到错误 验证视图状态 MAC 失败错误 ASP NET MVC 已尝试以下但没有运气 添加了机器密钥 http aspnetresources com tools machineKey 到 web co
  • Hyperledger Fabric 加密材料

    如果我们看到加密配置文件夹中基础网络 of 布料样品 我们有各种类型的各种证书材料 example com ca 0d46ccf0e9436c1bc3b6e2bf80cdb202c4943604f95c72ee0ff839d3ec30071
  • 由于名称中存在撇号而导致无效的 XPath 表达式异常

    我收到以下代码的无效 Xpath 异常 current Name current Name replace System out println current Name String xp1 page name current Name
  • 在 web.config 文件中设置重定向

    我正在尝试使用更具描述性的 URL 来重定向一些不友好的 URL 这些 URL 结尾为 aspx cid 3916每个类别名称页面的最后一位数字都不同 我希望它重定向到Category CategoryName 3916 我在web con
  • Android:如何创建“持续”通知?

    您好 我如何创建像第一个电池指示器一样的永久通知 如果您正在使用NotificationCompat Builder 您可以使用 NotificationCompat Builder mBuilder new NotificationCom
  • 从本地 html/javascript 网站发布到在线 PHP 文件

    我正在尝试做什么 从本地 html javascript 网站发布到在线 PHP 文件 Problem 当我尝试使用下面的代码时 我不断收到下面提到的错误 背景 该网站旨在本地运行 由于每个用户都可以选择使用哪个浏览器 因此我希望找到一种可
  • 将自定义计算添加到 magento 中的购物车总计和总计

    我正在网站上工作 我想在购物车总额和总计中添加 减去费用 我正在触发此事件以捕获购物车详细信息 sales order save after 在观察者中我使用此代码获得了价格 public function modifyPrice Vari
  • 使用 awk 或 sed 基于公共列合并两个 csv 文件 [重复]

    这个问题在这里已经有答案了 我有一个两个 CSV 文件 两个文件中有一个公共列 并且一个文件中有重复项 如何使用 awk 或 sed 合并两个 csv 文件 CSV 文件 1 5 1 20 user mark Type1 445566 5
  • 如何为for循环中除最后一项之外的每一项添加分隔符

    在下面的循环中 如何从循环中的latt键中删除逗号 var result These are the results jQuery each item keyterms terms function i kw for key in keyw
  • 访问 wpf c# 应用程序中其他类中 XAML 的按钮和复选框的值

    我正在开发 WPF Kinect 项目 它是 Windows Kinect 的开发人员工具包示例之一 称为 Kinect Explorer 您可以从 Kinect 开发者工具包 SDK 1 5 版下载它 在 kinectwindow xam
  • Angular:使用 Renderer2 添加 CSS 变量

    是否可以使用添加内联样式CSS变量Renderer2 我尝试了以下方法 但它不起作用 import Component OnChanges Output ViewChild Renderer2 ElementRef ViewEncapsul
  • Node CLI 工具评估字符串

    有没有办法使用 NodeJS CLI 工具来评估一串 Javascript 代码 例如 使用 Perl 将会是perl e code 使用Pythonpython c code 与红宝石ruby e code 并且使用 PHP php r
  • width:auto 对于 字段

    CSS新手问题 我想width auto for a display block元素的意思是 填充可用空间 然而对于一个
  • 从数据属性将字符串解析为对象[重复]

    这个问题在这里已经有答案了 我在使用 jQuery 验证插件时遇到了很多麻烦 解决这个问题的唯一方法是使用 submitHandler属性并在其中做一些技巧 其中检查触发器的父级是否是字段集以及是否有data submit handler属