0

我在这里有这个工作代码:

function updatetotal (section, pricetoadd)         
{
    TotalPrice[section] = pricetoadd;
    total = 0;

    for(key in TotalPrice)
    {
        total = total + TotalPrice[key];
    }

    $("#monthlycost").html("$" + total);
    $("#firstpayment").html("$" + total);
}

<input {if $hw_s.disk[$_var].content == $__val.content}checked{/if} type=radio name="order[disk][{$_var}][content]" value="{$__val.content}" price="{$__val.price}" setup="{$__val.setup}" onclick="updatetotal('disk{$_var}', {$__val.price})">

请注意,该值非常重要。它必须是 $__val.content 才能使所有后端代码正常运行。

我想要做的是将上面转换为下拉菜单并让功能仍然正常工作。JS 函数保持每个选定项目的运行总计并将它们相加。使用输入类型的收音机非常简单,因为 JS 函数可以采用价格变量。不幸的是,通过下拉菜单(据我所知),onChange 函数必须与选择相关联,而不是与选项相关联。不幸的是,由于选项是动态生成的,因此 select 对选项一无所知。

到目前为止的代码(不起作用):

function updatetotaldd(section, elementid)
{
    var selectBox = document.getElementById(elementid);
    var selectedValue = selectBox.options[selectBox.selectedIndex].value; // does not get what I need since value is a string. I need to somehow access the price of the selected option.


    TotalPrice[section] = selectedValue;

    for(key in TotalPrice)
    {
        total = total + TotalPrice[key];
    }

    $("#monthlycost").html("$" + total);
    $("#firstpayment").html("$" + total);
}

<select class="tech" name="order[disk][{$_var}][content]" id="svtech{$_var+1}" data-enablecheckbox="false" onChange="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
    {foreach key=__var item=__val from=$_val}
        <option {if $hw_s.disk.content == $__val.content}selected{/if} value="{$__val.content}"  price="{$__val.price}" setup="${$__val.setup}" onSelect="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
        {$__val.content}
        {if $__val.price > 0 && $__val.setup > 0}
            <strong> - add ${$__val.price}/Mo + ${$__val.setup} One time fee</strong>
        {elseif $__val.price > 0}
            <strong> - add ${$__val.price}/Mo</strong>
        {elseif $__val.setup > 0}
            <strong>- add ${$__val.setup} One time fee</strong>
        {/if}
        </option>
    {/foreach}
</select>

我怎样才能将价格传递给javascript(无论是选择上的onChange,还是选项本身的一些其他操作),或者以某种方式让选项具有可变属性(NOT VALUE)?

4

2 回答 2

2

fletch 是对的,我纠正了错误。见这里

var selectedOption = selectBox.options[selectBox.selectedIndex]; //need to somehow access the price of the selected option.

price= parseInt(selectedOption.getAttribute('price'));
于 2013-06-05T01:26:21.903 回答
0

只是把它扔在那里,以防你没有尝试过......

您已经price在元素上定义了属性option,因此您应该能够像任何其他属性一样检索它。不确定您是否需要调用onSelect每个option元素,因为onChangeonselect元素应该可以解决问题。

function updatetotaldd(section, elementid)
{
    var selectBox = document.getElementById(elementid);
    //var selectedValue = selectBox.options[selectBox.selectedIndex].value; // does not get what I need since value is a string. I need to somehow access the price of the selected option.
    var selectedPrice = selectBox.options[selectBox.selectedIndex].attributes.price.value;


    TotalPrice[section] = selectedPrice;

    for(key in TotalPrice)
    {
        total = total + TotalPrice[key];
    }

    $("#monthlycost").html("$" + total);
    $("#firstpayment").html("$" + total);
}

<select class="tech" name="order[disk][{$_var}][content]" id="svtech{$_var+1}" data-enablecheckbox="false" onChange="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
    {foreach key=__var item=__val from=$_val}
        <option {if $hw_s.disk.content == $__val.content}selected{/if} value="{$__val.content}"  price="{$__val.price}" setup="${$__val.setup}" onSelect="updatetotaldd('disk{$_var}', 'svtech{$_var+1}')">
        {$__val.content}
        {if $__val.price > 0 && $__val.setup > 0}
            <strong> - add ${$__val.price}/Mo + ${$__val.setup} One time fee</strong>
        {elseif $__val.price > 0}
            <strong> - add ${$__val.price}/Mo</strong>
        {elseif $__val.setup > 0}
            <strong>- add ${$__val.setup} One time fee</strong>
        {/if}
        </option>
    {/foreach}
</select>
于 2013-06-05T01:15:40.810 回答