假设我有这样的 JSON:
{
"foobar": "data foobar",
"foo": "data foo",
"bar": "data bar",
"baz": "data baz",
"qux": "data qux",
"quux": "data quux",
"corge": "data corge",
"grault": "data grault",
"garply": "data garply",
"waldo": "data waldo",
"fred": "data fred",
"plugh": "data plugh",
"xyzzy": "data xyzzy",
"thud": "data thud"
}
如何使用按键将数据填入输入框
let data = {
"foobar": "data foobar",
"foo": "data foo",
"bar": "data bar",
"baz": "data baz",
"qux": "data qux",
"quux": "data quux",
"corge": "data corge",
"grault": "data grault",
"garply": "data garply",
"waldo": "data waldo",
"fred": "data fred",
"plugh": "data plugh",
"xyzzy": "data xyzzy",
"thud": "data thud"
}
$('input[name=foobar]').val(data['foobar']);
$('input[name=foo]').val(data['foo']);
$('input[name=bar]').val(data['bar']);
...
可以看到,上面的代码给出了手动填充。我不想使用循环,但我想像这样获取相关的 jquery 选择器字符串的值:
$('input[name=foobar]').val(data[/=(\w+)\]/.exec(this.selectorstring)[1]]);
我期待
this.selectorstring
回报'input[name=foobar]'
。是否可以在 Javascript / jQuery 中执行此操作?