gpt4 book ai didi

javascript - 下拉列表元素包含子字符串/模式 : Disable element

转载 作者:行者123 更新时间:2023-11-30 14:00:28 25 4
gpt4 key购买 nike

我得到了一个下拉列表和选项,如下所述。

如果我尝试单击/选择“POICaption-XX423366 [DONOT ALLOW SELECT FOR THIS]”,我可以限制此值的选择。

    <select multiple="multiple" size=20 id="caption_collection">

<optgroup label="MAKE A CHOICE">
<option>SHOW ALL ENABLED CAPTIONS</option>
<option>SHOW ALL DISABLED CAPTIONS</option>
<option>SHOW ALL LISTED CAPTIONS</option>
</optgroup>
<optgroup label="ELSE SELECT ONE OR MULTIPLE">
<option>ALWCaption-A100104 [ALLOW SELECT HERE]</option>
<option>ZSDCaption-Z100104 [ALLOW SELECT HERE]</option>
<option>XCDCaption-S100104 [ALLOW SELECT HERE]</option>
<option>DCVCaption-Q100104 [ALLOW SELECT HERE]</option>
<option>ERTCaption-D100104 [ALLOW SELECT HERE]</option>
<option>BNMCaption-XX223366 [DO NOT ALLOW SELECT FOR THIS]</option>
<option>XWECaption-XX243356 [DO NOT ALLOW SELECT FOR THIS]</option>
<option>QWECaption-XX323356 [DO NOT ALLOW SELECT FOR THIS]</option>
<option>DFGCaption-XX228866 [DO NOT ALLOW SELECT FOR THIS]</option>
<option>TYUCaption-XX220066 [DO NOT ALLOW SELECT FOR THIS]</option>
<option>POICaption-XX423366 [DO NOT ALLOW SELECT FOR THIS]</option>
<option>GHJCaption-D100104 [ALLOW SELECT HERE]</option>
<option>LKICaption-D100104 [ALLOW SELECT HERE]</option>
<option>UYTCaption-XX423366 [DO NOT ALLOW SELECT FOR THIS]</option>
</optgroup>

</select>
<br><br>
<div>
<textarea id="selected" rows="4" cols="56" readonly></textarea>
</div>
const needle = '[DO NOT ALLOW SELECT FOR THIS]';
// using jQuery:
$('select option').prop('disabled', function() {
return this.text.includes(needle);
});

$("#caption_collection").change(function() {
$('#selected').html(' ');
$("#caption_collection option:selected").each(function() {
var $this = $(this);
if ($this.length) {
var selText = $this.text();
console.log(selText);
$('#selected').append(selText + ', ');
//$('#selected').text('Only Captions allowed are:').append(selText + ', ');
}
});

});

更新:更新了工作代码,但需要更多指导。

问题:在 optgroup ""MAKE A CHOICE"下有 3 个选项,如下所示

SHOW ALL ENABLED CAPTIONS
SHOW ALL DISABLED CAPTIONS
SHOW ALL LISTED CAPTIONS

我想要的是点击每个让我得到如下所示的值

SHOW ALL ENABLED CAPTIONS
A100104 ,Z100104 ,S100104 ,Q100104 ,D100104 ,D100104 ,D100104

SHOW ALL DISABLED CAPTIONS
XX223366 ,XX243356 ,XX323356 ,XX228866 ,XX220066 ,XX423366 ,XX423366

SHOW ALL LISTED CAPTIONS
XX223366 ,XX243356 ,XX323356 ,XX228866 ,XX220066 ,XX423366 ,XX423366, A100104 ,Z100104 ,S100104 ,Q100104 ,D100104 ,D100104 ,D100104

AND under optgroup label="ELSE SELECT ONE OR MULTIPLE"需要将一个或多个值显示为:

A100104  ,Z100104  ,S100104
A100104

虽然我可以提取

var stre = "LKICaption-A100104 [LKI   hfjdlfdlfjl]"
var str2 = stre.split('-');
var strval = str2[str2.length - 1];
alert(strval) // A100104 [LKI hfjdlfdlfjl] but I need only A100104

alert(strval.split(' ')[0]) // Worked this way o/p is A100104

最佳答案

你的想法是正确的。将 disablefor 字符串更改为以下内容,因为这是我们要查找的子字符串:

var disablefor= "[DO NOT ALLOW SELECT FOR THIS]";

使用 includes 方法检查子字符串:

if($thisOption.val().includes(disablefor)) {
$thisOption.attr("disabled", "disabled");
}

关于javascript - 下拉列表元素包含子字符串/模式 : Disable element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com