使用带有Javascript的两个选择菜单动态更新文本输入字段 [英] Update text input field dynamically using two select menus with Javascript

查看:59
本文介绍了使用带有Javascript的两个选择菜单动态更新文本输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚当用户更改一个或两个HTML选择菜单中的选项时如何动态更新文本输入字段.我在下面包含了显示其当前工作方式的代码.

I am trying to figure out how to dynamically update a text input field when a user changes the option in one or two HTML select menus. I have included my code below showing how it currently works.

$('tickets').addEvent('change', function() {
    if($('tickets').value > 0)
    {
        var cells = $$('.ticket2');
        cells.each(function(cell) { cell.setAttribute('style','display:none;');});
        var cells = $$('.ticket3');
        cells.each(function(cell) { cell.setAttribute('style','display:none;');});
        var sumValue = '$' + (100 * $('tickets').value + 10 * $('fiftytickets').value) + '.00';
        $('ordertotal').value = sumValue;
        $('ticket1heading').setHTML('Ticket(s)');
    } else {
        var cells = $$('.ticket2');
        cells.each(function(cell) { cell.setAttribute('style','');});
        var cells = $$('.ticket3');
        cells.each(function(cell) { cell.setAttribute('style','');});
        $('ticket2heading').setAttribute('style','text-align:center; font-weight:bold;');
        $('ticket3heading').setAttribute('style','text-align:center; font-weight:bold;');
        $('ordertotal').value = '$' + 250 + '.00';
        $('ticket1heading').setHTML('Ticket 1');
    }
});

票证选择菜单会正确影响订单总数文本输入字段,但五十票证选择菜单不会.我需要两者相互独立地工作,但是当它们各自更改时,要影响ordertotal文本输入字段的值.

The tickets select menu correctly affects the ordertotal text input field, but the fiftytickets select menu does not. I need the two to work independently of each other, but when each is changed, to affect the value of the ordertotal text input field.

我们非常感谢您的协助.

Any assistance is greatly appreciated.

谢谢.

迈克

推荐答案

,select元素的对象是用$('#tickets')而不是$('tickets')引用的. 另外,我认为jquery中没有select属性的value属性. 您可以使用val()函数获取值.

as said by JoDev, the select element's object is referenced with $('#tickets') and not just $('tickets'). also, I don't think that there is a value property in jquery for a select element. you can get the value with the val() function.

在这里在这里

$('#tickets').change(function() {
   $('#output').val($(this).val() + " - " + $('#fiftytickets').val());
});
$('#fiftytickets').change(function() {
   $('#output').val($('#tickets').val() + " - " + $(this).val());
});

这篇关于使用带有Javascript的两个选择菜单动态更新文本输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆