Javascript 使用 JQuery 获取所有数据绑定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11052806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting All Data-Bind Values Using JQuery
提问by LmC
function getDbValue()
{
alert($('[data-bind]').length);
alert($('[data-bind][0].data-bind'));
alert($('[data-bind][0].value'));
jQuery.each($('[data-bind]'), function(databind,key)
{
alert(key);
alert(databind);
alert(databind[key].data-bind);
})
}
The above is my function and i want to read all inputs that have the properties data-bind within them for example
上面是我的函数,我想读取所有包含数据绑定属性的输入,例如
<input type="text" id="frmIn1-Officer" data-bind="value: AOfficer" class="InputText"/>
^ When running my function i would want it to return 'AOfficer' as that is the data-bind value.
^ 运行我的函数时,我希望它返回“AOfficer”,因为这是数据绑定值。
So an example is
所以一个例子是
<input type="text" id="frmIn1-Officer" data-bind="value: AOfficer1" class="InputText"/>
<input type="text" id="frmIn1-Officer" data-bind="value: AOfficer2" class="InputText"/>
<input type="text" id="frmIn1-Officer" data-bind="value: AOfficer3" class="InputText"/>
<input type="text" id="frmIn1-Officer" data-bind="value: AOfficer4" class="InputText"/>
<input type="text" id="frmIn1-Officer" data-bind="value: AOfficer5" class="InputText"/>
<input type="text" id="frmIn1-Officer" data-bind="value: AOfficer6" class="InputText"/>
And in the for each loop i would like to be able to use the value of data bind.. e.g values[0] = 'AOfficer1'
在 for each 循环中,我希望能够使用数据绑定的值.. 例如 values[0] = 'AOfficer1'
Sorry if my explanation is slightly confusing, i have the idea in my head perfect but trying to put it in writing is alot harder.
对不起,如果我的解释有点令人困惑,我的想法是完美的,但试图把它写下来要困难得多。
回答by Milimetric
jQuery interprets the "data-something" attributes differently than other attributes. So you should select all your elements and look for their data bindings like this:
jQuery 对“data-something”属性的解释与其他属性不同。因此,您应该选择所有元素并查找它们的数据绑定,如下所示:
$(document).ready(function(){
$('input.InputText').each(function(){
var input = $(this);
if ($(input).data().bind) {
alert($(input).data().bind);
}
});
});?
Then you can do string manipulation to parse out your values, I'd suggest using JSON and just loading it in like an object. Here's a working fiddle: http://jsfiddle.net/3NERK/6/
然后你可以做字符串操作来解析你的值,我建议使用 JSON 并像对象一样加载它。这是一个工作小提琴:http: //jsfiddle.net/3NERK/6/
回答by Dvir Azulay
You can search for any element that has a data-bind
attribute by the jQuery attribute selector - $("[data-bind]")
, and then iterate on it with .each()
and construct the dataBinds
array out of it, stripping the value:
out of each value.
您可以data-bind
通过 jQuery 属性选择器 -搜索具有属性的任何元素,$("[data-bind]")
然后使用它进行迭代并从中.each()
构造dataBinds
数组,value:
从每个值中剥离出来。
This will do the trick:
这将解决问题:
dataBinds = [];
$("[data-bind]").each(function(){
dataBinds.push($(this).attr("data-bind").substring(7));
});??????
I've set up an example of it: http://jsfiddle.net/dvirazulay/YPnwQ/
我已经建立了一个例子:http: //jsfiddle.net/dvirazulay/YPnwQ/
回答by Esailija
$( "[data-bind]" ).each( function() {
var elem = $( this );
alert( elem.data( "bind" ) );
});
回答by Rafael Verger
Get all elements with data-bind
attribute: $('[data-bind]')
获取所有具有data-bind
属性的元素:$('[data-bind]')
Iterating these elements and manipulating the data-bind attribute:
迭代这些元素并操作 data-bind 属性:
$('[data-bind]').each(function(element,index){
var data_bind = $(element).data('bind');
alert(data_bind);
})