[英]jquery attribute selector is not working for data-attribute
I know it could be duplicate of SO questions Selecting element by data attribute and jQuery how to find an element based on a data-attribute value?我知道它可能与 SO 问题Selecting element by data attribute和jQuery how to find an element based on a data-attribute value?重复
But still I am not able to select all elements where an attribute "data-value" exist.但我仍然无法 select 存在属性“数据值”的所有元素。
Issue: I am expecting statement B) $('[data-value]')
or $('*[data-value]*')
should return all elements which have data attribute "data-value", but it returns nothing.问题:我期望语句 B) $('[data-value]')
或$('*[data-value]*')
应该返回所有具有数据属性“data-value”的元素,但它什么也不返回。
However statement A) $('#openingBalAccount').data('value')
getting result "ACC-2".但是语句 A) $('#openingBalAccount').data('value')
得到结果“ACC-2”。
If statement A above works, then statement B should also work, but its not happening.如果上面的陈述 A 有效,那么陈述 B 也应该有效,但它没有发生。
Below is the Chrome Dev Tool screen for the same, consider first 2 statements in screen as Statement A above, and rest as Statement B. As stated above if statement A works,statement B also should work.下面是相同的 Chrome 开发工具屏幕,将屏幕中的前 2 个语句视为上面的语句 A,将 rest 视为语句 B。如上所述,如果语句 A 有效,则语句 B 也应该有效。
Dev Tool example should be more than sufficient to identify any issue, but if you still insist for the code, here it is.开发工具示例应该足以识别任何问题,但如果您仍然坚持要代码,请看这里。
Jquery Statement to set data-value Jquery 设置数据值的语句
$('#accountsModalSelect').on('click', function onAccountsModalSelect() {
let rowData = $('#accountsModalSelect').data('selected')
if (rowData) $('.accountSource').val(rowData.description).data('value', rowData.account)
$('#accountsModal').modal('hide')
})
HTML Code in pug format HTML 哈巴狗格式的代码
form#openingBalForm
input.form-control#openingBalUpdateType(type='hidden')
input.form-control#formtab(type='hidden' value='openingbaltab')
.row.form-group
.col-4
labal(for='openingBalFinYear') Financial Year
.col
input.form-control#openingBalFinYear(name='openingBalFinYear' type='text' )
.row.form-group
.col-4
labal(for='openingBalAccount') Account
.col
.input-group
input.form-control.accountSource#openingBalAccount(name='openingBalAccount' type='text' )
.input-group-append#openingBalAccountSearch
.input-group-text
.fas.fa-search
.row.form-group
.col-4
labal(for='openingBalAmount') Amount
.col
input.form-control#openingBalAmount(name='openingBalAmount' type='number')
If you want to do something like this, you also have to add the html-attribute to the element.如果你想做这样的事情,你还必须将 html-attribute 添加到元素中。 You can't access the internal stored data.您无法访问内部存储的数据。
$('#accountsModalSelect').on('click', function onAccountsModalSelect() {
let rowData = $('#accountsModalSelect').data('selected')
if (rowData) {
$('.accountSource')
.val(rowData.description)
.data('value', rowData.account)
.attr('data-value', rowData.account) // Add this
}
$('#accountsModal').modal('hide')
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.