我知道使用.value可以获得所选项目的值,但我需要的是所选项目的名称。例如,我有一个包含两个项目的下拉菜单: Class 1和Class 2。Class 1的值为50,Class 2的值为60。如果我使用DMT5.value (Class 1的值),我会得到50,但我需要的是名称本身,即"Class 1“。因此,我尝试使用以下代码:
var classRate = document.getElementById("DMT5")
var selectedClassRate = classRate.options[classRate.selectedIndex].text但是,我得到一个错误:无法获取未定义或空引用的属性'text‘
*编辑:这里是DMT5:
<td style="padding-right: 9px;">
<select id="DMT5" onchange="jCalculate()" style="width: 143px; padding: 2px 5px 2px 5px;">
<option></option>
<script runat="server" language="cache">
do INIT^CSP999
#import MX
set Origin=$g(%session.Data("VIPRates","Origin"))
set ID=$g(%session.Data("VIPRates","ID"))
&sql(DECLARE cCursor0 CURSOR FOR
SELECT VIPRatesItems_Class1, VIPRatesItems_Class2
INTO :r1,:r2
FROM MX.VIPRates_VIPRatesItems
WHERE VIPRates->Customer->CustCode=:ID AND VIPRatesItems_Country=:Origin)
&sql(OPEN cCursor0)
&sql(FETCH cCursor0)
WHILE SQLCODE=0{
w "<option value='"_r1_"'>Class 1</option>"
w "<option value='"_r2_"'>Class 2</option>"
&sql(FETCH cCursor0)
}
&sql(CLOSE cCursor0)
</script>
</select> </td>
</tr>这是我的JS:
function jCalculate(){
document.getElementById("s1").style.display="inline"
document.getElementById("s2").style.display="inline"
document.getElementById("s4").style.display="inline"
var UnitMeasure="kg"
var Country=PackageT8.value
var Dm = 3500
repack = 0
Dimension=0
Volumetric=0
SelectedWt=0
OtherCharges=0
if (Country == "UNITED STATES OF AMERICA") {
var Class1 = DMT5.value
var Dm = 166
}
else if (Country == "UNITED KINGDOM") {
var Class1 = DMT5.value
Dm=6000
alert(DMT5.value)
}
else if (Country == "HONG KONG") {
var Class1 = DMT5.value
}
else if (Country == "CHINA") {
var Class1 = DMT5.value
}
else if (Country == "SINGAPORE") {
var Class1 = DMT5.value
}
if (Country == "UNITED STATES OF AMERICA") {
var Class2 = DMT5.value
}
else if (Country == "UNITED KINGDOM") {
var Class2 = DMT5.value
}
else if (Country == "HONG KONG") {
var Class2 = DMT5.value
}
else if (Country == "CHINA") {
var Class2 = DMT5.value
}
else if (Country == "SINGAPORE") {
var Class2 = DMT5.value
}
var Volumetric= (DMT1.value*DMT2.value*DMT3.value)/Dm
if (isNaN(Volumetric)) var Volumetric=0
if(Volumetric % 1 != 0) Volumetric=Volumetric+1
var Volumetric=parseInt(Volumetric)
if (DMT5.selectedIndex=="1") var ClassAmt=Class1
if (DMT5.selectedIndex=="2") var ClassAmt=Class2
var ActualWt=DMT6.value
if (isNaN(ActualWt)) var ActualWt=0
if (ActualWt>Volumetric)var SelectedWt=ActualWt
if (ActualWt<=Volumetric)var SelectedWt=Volumetric
var VAT=(SelectedWt*ClassAmt)*(12/100)
if (isNaN(VAT)) var VAT=0
if (CB1.checked==true){
var repack=3
}
var AmountDue=(SelectedWt*ClassAmt)+ VAT
if (isNaN(AmountDue)) var AmountDue=0
if (Country=="U.S.A"){
var UnitMeasure="lb/s"
}
DMT9.value=SelectedWt
DMT11.value=ClassAmt
DMT12.value=Volumetric
document.getElementById("AmountDue").innerHTML=AmountDue.toFixed(2)
document.getElementById("s1").innerHTML="Volumetric Weight: "+Volumetric + UnitMeasure
document.getElementById("s2").innerHTML="Chargable Weight: "+SelectedWt + UnitMeasure
document.getElementById("s3").style.display="inline"
//----OTHER CHARGES
if (Country=="U.S.A"){
var Volumetric=((DMT1.value * 2.54) * (DMT2.value * 2.54) * (DMT3.value * 2.54))/3500
if (isNaN(Volumetric)) var Volumetric=0
if(Volumetric % 1 != 0) Volumetric=Volumetric+1
var Volumetric=parseInt(Volumetric)
var ActualWt=DMT6.value
ActualWt=ActualWt/2.2
if(ActualWt% 1 != 0) ActualWt=ActualWt+1
var ActualWt=parseInt(ActualWt)
if (isNaN(ActualWt)) var ActualWt=0
if (ActualWt>Volumetric)var SelectedWt=ActualWt
if (ActualWt<=Volumetric)var SelectedWt=Volumetric
}
if (SelectedWt<=3 ){
if (Country=="U.S.A"){
Dimension = ((DMT1.value * 2.54) * (DMT2.value * 2.54) * (DMT3.value * 2.54))/3500
}
else{
Dimension = (DMT1.value * DMT2.value * DMT3.value)/3500
}
if (isNaN(Dimension )) var Dimension = 0
if(Dimension % 1 != 0) Dimension =Dimension +1
var Dimension =parseInt(Dimension )
if ((DMT4.value=="MMnl" || DMT4.value=="Luzon") && (Country!="U.S.A")){
var OtherCharges = 4.48
}
else if ((DMT4.value=="Visayas" || DMT4.value=="Mindanao") && (Country!="U.S.A")){
var OtherCharges = 4.62
}
else if(DMT4.value=="Luzon"){
var OtherCharges = 4.48
}
else if (DMT4.value=="Visayas" || DMT4.value=="Mindanao"){
var OtherCharges = 4.62
}
}
if (SelectedWt>=4){
if (Country=="U.S.A"){
Dimension = ((DMT1.value * 2.54) * (DMT2.value * 2.54) * (DMT3.value * 2.54))/3500
}
else{
Dimension = (DMT1.value * DMT2.value * DMT3.value)/3500
}
if (isNaN(Dimension )) var Dimension =0
if(Dimension % 1 != 0) Dimension =Dimension +1
var Dimension =parseInt(Dimension )
if ((DMT4.value=="MMnl" || DMT4.value=="Luzon") && (Country!="U.S.A")){
var OtherCharges = 2.30 * Dimension
}
else if ((DMT4.value=="Visayas" || DMT4.value=="Mindanao") && (Country!="U.S.A")){
var OtherCharges = 2.50 * Dimension
}
else if(DMT4.value=="Luzon"){
var OtherCharges = 2.30 * Dimension
}
else if (DMT4.value=="Visayas" || DMT4.value=="Mindanao"){
var OtherCharges = 2.50 * Dimension
}
}
if (Country=="U.S.A" && DMT4.value=="MMnl"){
var OtherCharges=0
}
if (PaymentT3.value=="For Pick Up"){
var OtherCharges=0
}
var AmountDue=AmountDue+OtherCharges+repack
if (isNaN(AmountDue)) var AmountDue=0
DMT7.value=VAT.toFixed(2)
DMT8.value=OtherCharges.toFixed(2)
#server(..cAmountInPeso(AmountDue.toFixed(2),DMT13.value,DMT14.value))#
document.getElementById("s4").innerHTML="Delivery Charges: USD "+ OtherCharges.toFixed(2)
}我想我已经用'selectedIndex‘解决了这个问题。我获取所选项目的索引。
发布于 2017-10-06 14:31:50
使用getAttribute
classRate.options[classRate.selectedIndex].getAttribute("name");发布于 2017-10-06 14:43:06
if(classRate.options.length > classRate.selectedIndex)
var selectedClassRate = classRate.options[classRate.selectedIndex].text; https://stackoverflow.com/questions/46599556
复制相似问题