0

そうですね、3 つのドロップダウン ボックスに値があり、さらに値が追加されます (アイテムの最終価格を示すために追加する約 20 のドロップダウン ボックスを追加することを検討しています。HTML のコードは次のとおりです。

<form id="myform1>
<p>Testing 1</p>
<select id="Test1">
<option value="24">Item one</option>
<option value="29">Item Two<option>
<!--I have the first box going to 20 items-->
</select>

<p>Testing 2</p>
<select id="Test2">
<option value="25">Item one</option>
<option value="100">Item Two<option>
<!--I have the second box going to 10 items-->
</select>

<p>Testing 3</p>
<select id="Test3">
<option value="24">Item one</option>
<option value="100">Item Two<option>
<!--I have the third box going to 10 items-->
</select>


</form>
<button onclick="myFunction()">Update</button>
<p id="demo"></p>

それから私は私のJavascriptを持っています

function myFunction()
{
var a = document.getElementById("list1");
var A = a.options[a.selectedIndex].value;

var b = document.getElementById("list2");
var B = b.options[a.selectedIndex].value;

var c = document.getElementById("list3");
var C = c.options[a.selectedIndex].value;

var IntA = parseInt(A);
var IntB = parseInt(B);
var IntC = parseInt(C);

var x=IntB + IntA;
var y=x + IntC;
var demoP=document.getElementById("demo")
demoP.innerHTML= y;
}

私の問題は、var c を完全に取り出してから var y を取り出して x に計算する場合にのみ機能することです。私は多くの方法を試しましたが、途方に暮れています。乾杯

PS Java スクリプトは、下部の script タグにあります。

4

2 に答える 2

3

a.selectedIndex3 つの要素すべてについて、選択した項目を取得するために使用しました。b.selectedIndexそれをc.selectedIndex適切な場所で使用するように変更します。または、コレクションを経由するのではなく、直接a.valueb.valueand と言うことができます。c.valueoptions

また、html の id 属性は、JS で使用しているものと一致しません。

ここでわかるように、これらの問題を修正すると動作します: http://jsfiddle.net/6WWdc/

parseInt()PS のように、基数を 2 番目のパラメーターとして常に渡す習慣を身に付けるのは良い考えです。IntA = parseInt(A, 10);そうしないと、入力文字列に先行0または先行がある場合、 0x(ブラウザーや"use strict";ディレクティブの存在に応じて) 8 進数として解釈される可能性があります。または16進数。

于 2013-02-11T21:03:23.417 に答える
0
function myFunction()
{
    var a = document.getElementById("list1");
    var A = a.options[a.selectedIndex].value;

    var b = document.getElementById("list2");
    var B = b.options[b.selectedIndex].value; //changed to var b

    var c = document.getElementById("list3");
    var C = c.options[c.selectedIndex].value; //changed to var c

    //...
}
于 2013-02-11T21:05:02.977 に答える