通过JavaScript更改样式颜色
当选择某个选项但不起作用时,我一直在尝试更改输入字段的样式颜色。我尝试了OnClick和Onput,但无济于事。我怀疑这个问题在于{{type}}或=='buy'。
function changeColor () {
var transaction_type = document.getElementById('buysell');
var option_user_selection = transaction_type.options[ transaction_type.selectedIndex ].text;
if (option_user_selection == 'Buy') {
document.getElementById('costb').style.color = '#006400';
}
else {
document.getElementById('costb').style.color = '#DC143C';
}
}
<div class="transaction_type">
<p>Transaction Type:</p>
<select style="font-size:16px" class="form-select" id='buysell' required>
{% for type in form.transaction_type %}
<option>{{ type }}</option>
{% endfor %}
</select>
</div>
<div>
<p>Cost Basis ($):</p>
{{ form.cost_basis }}
</div>
forms.py
transaction_choices = [
('BUY', 'Buy'),
('SELL', 'Sell'),
]
transaction_type = forms.CharField(
max_length=4,
widget=forms.Select(
choices=transaction_choices,
attrs={
'class': 'form-select',
'name': 'transaction_type',
'id': 'floatingTransaction',
'oninput': 'changeColor();'
})
)
cost_basis = forms.DecimalField(
min_value = 0,
widget = forms.NumberInput(attrs={
'class': 'form-control',
'name': 'cost_basis',
'id': 'costb',
})
)
I have been trying to change the style color of an input field when a certain option is selected but it is not working. I have tried both onclick and oninput but to no avail. I suspect the issue lies with {{ type }} or == 'Buy'.
function changeColor () {
var transaction_type = document.getElementById('buysell');
var option_user_selection = transaction_type.options[ transaction_type.selectedIndex ].text;
if (option_user_selection == 'Buy') {
document.getElementById('costb').style.color = '#006400';
}
else {
document.getElementById('costb').style.color = '#DC143C';
}
}
<div class="transaction_type">
<p>Transaction Type:</p>
<select style="font-size:16px" class="form-select" id='buysell' required>
{% for type in form.transaction_type %}
<option>{{ type }}</option>
{% endfor %}
</select>
</div>
<div>
<p>Cost Basis ($):</p>
{{ form.cost_basis }}
</div>
forms.py
transaction_choices = [
('BUY', 'Buy'),
('SELL', 'Sell'),
]
transaction_type = forms.CharField(
max_length=4,
widget=forms.Select(
choices=transaction_choices,
attrs={
'class': 'form-select',
'name': 'transaction_type',
'id': 'floatingTransaction',
'oninput': 'changeColor();'
})
)
cost_basis = forms.DecimalField(
min_value = 0,
widget = forms.NumberInput(attrs={
'class': 'form-control',
'name': 'cost_basis',
'id': 'costb',
})
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论