【问题标题】:Select option doesn't load correctly while trying to edit a modal尝试编辑模式时选择选项无法正确加载
【发布时间】:2021-07-26 00:24:24
【问题描述】:

当我尝试从表格进行编辑并将值转换为模态表单时遇到了这个问题,一切正常,但有时无法加载选项值。

这是我用来获取值并加载它们的代码部分

$("#nombrePlazaUS > option[value=" + idLocacion + "]").attr("selected", true);
$("#nombreTipoUS > option[value=" + idTipoUser + "]").attr("selected", true);
$("#descripcionUS > option[value=" + idtipodePersona + "]").attr("selected", true);

在 HTML 中你可以看到我使用了value="0" 作为默认值:

<div class="labelModal">
    <label>Locacion</label>
    <select id="nombrePlazaUS" name="nombrePlazaUS">
        <option value="0" selected>Seleccione la locacion</option>
    </select>
</div>
<div class="labelModal">
    <label>Tipo de Empleado</label>
    <select id="nombreTipoUS" name="nombreTipoUS">
        <option value="0" selected>Seleccione el tipo de usuario</option>
    </select>
</div>
<div class="labelModal">
    <label>Tipo de usuario</label>
    <select id="descripcionUS" name="descripcionUS">
        <option value="0" selected>Seleccione el tipo de empleado</option>
    </select>
</div>

Sometimes works like it should

但是有一半的时间值不能正确加载,一切都可能是正确的,或者有时 1 或 2 甚至 3 个值都不能加载,完全随机,但是当它们加载时,是正确的值。

In this case 2 of the values didn't load and they have the default value

【问题讨论】:

  • 您选择的 HTML options 在您显示的 HTML 中不存在。所以我猜你正在动态添加它们。该代码在哪里,它何时运行与选择option 的代码相关? When the select fails, have you verified that the HTML options were really added at all?请尝试创建minimal, complete, and verifiable example

标签: javascript html jquery html-select options


【解决方案1】:

您应该使用取消选择所有其他选项 .attr("selected", false); 在您选择之前。

【讨论】:

    猜你喜欢
    • 2021-05-14
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多