我對此有意見
attr('data-disabled-dates','12/02/2022; 13/02/2022; 14/02/2022; 15/02/2022; 10/03/2022; 11/03/2022; 16/02/2022')
還有一個 const 會動態地導致明天,所以對于這種情況,結果是“16/02/2022”
現在,如果它與明天的日期在 attr data-disabled-dates 內匹配,我想運行操作。
所以我嘗試了這個
if (jQuery(input).attr('data-disabled-dates') == '16/02/2022')
{ console.log('work') } else {
console.log ('not') }
但它只有在整個序列完全相同的情況下才給我真實的情況,也就是說,如果我輸入“12/02/2022;13/02/2022...”如果它會給出結果,但我只希望它如果我輸入的值在里面,則為真
uj5u.com熱心網友回復:
您可以使用String.includes()orString.split('; ')將字串轉換為陣列并檢查它是否包含所需的值。
if (jQuery(input).attr('data-disabled-dates').includes('16/02/2022')) {
console.log('work')
} else {
console.log('not')
}
vanilla JS中的作業示例
const div = document.querySelector('#data');
const dates = div.getAttribute('data-disabled-dates');
if (dates.includes('16/02/2022')) {
console.log('work')
} else {
console.log('not')
}
// or
if (dates.split('; ').includes('16/02/2022')) {
console.log('work')
} else {
console.log('not')
}
<div id="data" data-disabled-dates="12/02/2022; 13/02/2022; 14/02/2022; 15/02/2022; 10/03/2022; 11/03/2022; 16/02/2022" />
順便說一句https://youmightnotneedjquery.com/
uj5u.com熱心網友回復:
==執行精確的字串匹配,而不是子字串匹配。
您可以使用.split()將屬性值拆分為一個陣列,然后用于.includes()測驗該陣列是否包含日期。
if (jQuery(input).data('disabled-dates').split('; ').includes('16/02/2022')) {
console.log("work");
} else {
console.log("not");
}
uj5u.com熱心網友回復:
如果需要支持 IE,可以使用 indexOf 大于 0。IndexOf。
var dataAttrValue = jQuery(input).attr('data-disabled-dates');
var date = '16/02/2022';
if (typeof dataAttrValue !== 'undefined' && dataAttrValue.indexOf(date)) > 0) { console.log('work');
} else {
console.log ('not');
}
或者
jQuery(input).is(“[data-disabled-dates*='16/02/2022']”);
但我建議使用 JQuery Data 來存盤和檢索元素中的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/425077.html
標籤:javascript jQuery if 语句 包含 属性
下一篇:帶影像旋轉的縮放計算
