使用JQuery获取所有数据绑定值 [英] Getting All Data-Bind Values Using JQuery
问题描述
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"/>
^运行我的函数时,我希望它返回"AOfficer",因为那是数据绑定值.
^ When running my function i would want it to return 'AOfficer' as that is the data-bind value.
所以一个例子是
<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"/>
在for每个循环中,我希望能够使用数据绑定的值. 例如values [0] ='AOfficer1'
And in the for each loop i would like to be able to use the value of data bind.. e.g 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.
推荐答案
jQuery对数据内容"属性的解释与其他属性不同.因此,您应该选择所有元素并查找它们的数据绑定,如下所示:
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:
$(document).ready(function(){
$('input.InputText').each(function(){
var input = $(this);
if ($(input).data().bind) {
alert($(input).data().bind);
}
});
});
然后,您可以进行字符串操作以解析出您的值,我建议您使用JSON并将其像对象一样加载.这是一个有效的小提琴: http://jsfiddle.net/3NERK/6/
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/
这篇关于使用JQuery获取所有数据绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!