简体   繁体   English

如何将新添加的 select 选项暴露给 Javascript

[英]How to expose newly added select option to Javascript

I've spent a while on this and can't find an answer that makes sense to me.我在这上面花了一段时间,找不到对我有意义的答案。 Not saying there's not 100s of answers out there which would help, I just haven't picked a solution out.并不是说那里没有 100 个答案会有所帮助,我只是没有选择解决方案。

I have a select field within a jinja template, hooked up to a Python / Flask / SQLAlchemy application. I have a select field within a jinja template, hooked up to a Python / Flask / SQLAlchemy application. If it makes any difference, I'm using Bootstrap 5. I am only just starting to get to grips with Javascript.如果有什么不同,我正在使用 Bootstrap 5。我才刚刚开始掌握 Javascript。

The behaviour I'm aiming for is: -我的目标是: -

  • From a template, click on a link to open up a Bootstrap 5 modal - works fine从模板中,单击链接以打开 Bootstrap 5 模式 - 工作正常
  • Enter details into the modal form and save the details - works fine在模态表单中输入详细信息并保存详细信息 - 工作正常
  • When I return to the template - the new entry appears as a selectable option - works fine当我返回模板时 - 新条目显示为可选选项 - 工作正常
  • I also want the select field to default to the newly added option - not working (the field remains blank)我还希望 select 字段默认为新添加的选项 - 不起作用(该字段保持空白)

After investigating, it seems as though the new option, although visible in the select field, isn't yet available to Javascript.经过调查,似乎新选项虽然在 select 字段中可见,但对 Javascript 尚不可用。

Code for the select field (based on an SQLAlchemy QuerySelect field): - select 字段的代码(基于 SQLAlchemy QuerySelect 字段):-

HTML (Jinja) HTML(神社)

# I'm using wtforms so the id of this element is #individual_birth_location
<div class="col-sm-7 w-auto">
    {{ wtf.form_field(form.individual_birth_location, class='form-select w-auto') }}
</div>

Javascript Javascript

    <!--The 'btnaddlocation' button is the Save button on the modal-->
    document.getElementById('btnaddlocation').onclick = function() {
    var len = document.getElementById('individual_birth_location').length;      
    if (eventType === 'birth') {
        document.getElementById('individual_birth_location').value = len;
    } else if (eventType === "death") {
        <!-- similar code-->
    }
    }

I've tried setting the value to len-1 (ie the option added immediately prior to the new option) and this works fine.我尝试将值设置为 len-1 (即在新选项之前添加的选项)并且效果很好。

But when I add a new record via the modal, it seems the template doesn't "know" about the new record yet.但是当我通过模态添加新记录时,模板似乎还不“知道”新记录。 So the select field remains blank (even though the newly added option is clearly in the list).所以 select 字段保持空白(即使新添加的选项清楚地在列表中)。

What am I missing?我错过了什么?

Thanks.谢谢。

I am not sure about python but我不确定 python 但是

   <!--The 'btnaddlocation' button is the Save button on the modal-->
    document.getElementById('btnaddlocation').onclick = function() {
    var len = document.getElementById('individual_birth_location').length;      
     if (eventType = 'birth') { 
         document.getElementById('individual_birth_location').value = len;
     } else if (eventType = "death") {
         <!-- similar code-->
    }
    }

The comparison that you are trying(assuming) is not comparing but it assigns.您正在尝试(假设)的比较不是比较而是分配。 So javascript would look like this if you want to check if eventType is equal to 'birth':因此,如果您想检查 eventType 是否等于“birth”,javascript 将如下所示:

document.getElementById('btnaddlocation').onclick = function() {
var len = document.getElementById('individual_birth_location').length;      
if (eventType === 'birth') { 
    document.getElementById('individual_birth_location').value = len;
} else if (eventType === "death") {
    <!-- similar code-->
}
}

Source for comparison w3schools比较来源 w3schools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何禁用一个<option>在另一个选择<select>在申请新增加的同时<select>和以前<select> - How do you disable an <option> selected in another <select> while applying to newly added <select> and previous <select> 如何将 select2 功能添加到新添加的动态选择框 - How to add select2 function to newly added dynamic select box 如何在 javascript 中选择 select 选项? - How to select option in javascript? 从具有多个属性的HTML选择中仅获取新添加和删除的选项? - Get only newly added and removed option from HTML select with multiple attribute? 如何为动态添加的select / option标签动态选择选项 - How to dynamically select the option for dynamically added select/option tag 选择(触发更改)Javascript/jQuery 中选择下拉列表的动态添加选项 - Select (trigger change) the dynamically added option of a select dropdown in Javascript/jQuery 如何通过javascript设置新添加的单元格的html属性 - How to set html properties of a newly added cell through javascript 如何获得对javascript类中新添加的函数的访问 - how to get access to newly added functions inside a javascript class javascript-如何调用从ajax新添加的函数 - javascript - how to call a function newly added from ajax 如何在JavaScript中为新添加的div指定其他宽度 - How do I specify another width for newly added divs in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM