gpt4 book ai didi

jquery - 我正在使用 jquery 选择的插件

转载 作者:行者123 更新时间:2023-12-01 07:52:12 25 4
gpt4 key购买 nike

从下拉列表中,我只想要某些部分(例如,AL_Alabhama 意味着我只想要 AL)。我使用 split 函数执行此操作。当我从下拉列表中选择某个值时,文本框包含拆分结果,但相同的值会反射(reflect)回下拉列表。我想要下拉列表中的原始值。请建议一些解决方案

        $(document).ready(function () {
var selText;
$('#locate option').each(function () {
$(this).attr('name', $(this).text());
});
$('#locate').change(function () {
//Gets the value for selected option
selText = $('#locate option:selected').text();
//split the value
$('#locate option:selected').text(selText.split("_")[0]).trigger('liszt:updated');
//trying to add the values again to the dropdown
$('#locate').next().click(function () {
if (selText == "") {
$("#locate option").filter(":contains(" + selText.split("_")[0] + ")").text(selText).trigger('liszt:updated');

});
}

最佳答案

看行$('#locate option:selected').text(selText.split("_")[0]).trigger('liszt:updated');

为了方便起见,让我们将其分成两部分:

var splittedText = selText.split("_")[0];
$('#locate option:selected').text(splittedText).trigger('liszt:updated');

你看到会发生什么吗?您无需将分割的文本分配给变量,而是将其设置为返回下拉列表的文本。

只需将其分配给一个变量,就像我上面向您展示的那样:var splittedText = selText.split("_")[0];

并删除 .text(splittedText) 部分

所以它看起来像这样:

var splittedText = selText.split("_")[0];
$('#locate option:selected').trigger('liszt:updated');

更新:

触发自定义事件.trigger('liszt:updated');只会通知所选插件您已更新下拉列表。如果你想单独设置文本框的文本,你只需手动设置:

$("#myTextBox").val(splittedText);

正如您可能知道(或不知道)的那样,选择器 $('#locate option:selected')将返回所有选定的列表 <option>父级为 id="locate" 的元素。

因此,由于您很可能一次只能选择 1 个选项,这将返回您在下拉列表中选择的选项。

当你调用函数.text()时jquery 对象并提供参数(例如: .text(splittedText) ),它将设置 jquery 选择器返回的列表中每个元素的文本。
如果您不希望选择选项更改其文本,请不要调用 .text(splittedText)

关于jquery - 我正在使用 jquery 选择的插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28495311/

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