问题描述:

i have a select option which works fine,but what i want is if more than one is selected then 10 % discount has to be added, how can this be done

and on selected the value multiplies in Java script

//javascript

function totprice(f,n,amt)

{

var tot=0;

document.getElementById('lbl_tot'+n).value=amt*f;

for (var k=1; k<=document.getElementById('rowcnt').value; k++)

{

if(document.getElementById('lbl_tot'+k))

{

tot=parseInt(tot) + parseInt(document.getElementById('lbl_tot'+k).value);

}

}

document.getElementById('txttot').value=tot;

document.getElementById('txttotprice').value=tot;

}

HTML

<select name="amt" id="amt" onchange="return totprice(this.value,2,110);">

<option value="0">0</option>

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

<option value="4">4</option>

<option value="5">5</option>

<option value="6">6</option>

<option value="7">7</option>

<option value="8">8</option>

<option value="9">9</option>

<option value="10">10</option>

<option value="11">11</option>

<option value="12">12</option>

<option value="13">13</option>

<option value="14">14</option>

<option value="15">15</option>

<option value="16">16</option>

<option value="17">17</option>

<option value="18">18</option>

</select>

网友答案:

Your select tag should be

<select name="amt" id="amt" onchange="return totprice(this.value,2,110);" multiple>

To make it multi select. Now when user will select multiple options (using Shift+click) then in your JavaScript function get these values and do the processing (provide discount/ any logic)

Updated
You wish to multiply the value the user has selected with the discount amount (110)
In your JS have this

var x = parseInt(f)*parseInt(amt);

If f = 2 and amt = 110 then x = 220. You need to do parseInt(value, radix) by default radix is 10

EXAMPLE:

var optionSelected = document.getElementById('amt').value;  //optionSelected is nothing but this.value
if(optionSelected > 1)
{
 //Offer discount
 var x = parseInt(f)*parseInt(amt);  //where f and amt is passed through function call
}
else
{
 // Do nothing/ other logic
}

Hope this helps!!

相关阅读:
Top