[英]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: -我的目标是: -
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-->
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.