通过JavaScript更改样式颜色

发布于 2025-02-13 22:34:47 字数 1633 浏览 0 评论 0原文

当选择某个选项但不起作用时,我一直在尝试更改输入字段的样式颜色。我尝试了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',
        })
)

Rendered Template

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文