非常感谢所有人的帮助 - 非常感谢。
然而,我接下来要做的事情真的很复杂,我确信你们有大脑帮助我。
这是我想以更好的方式编写的更复杂的代码 - 更少的代码,更高效。
很抱歉,这是很多代码。
JS:
function keyTechAdoption() {
var keyTechBox = document.getElementById('keyTechTextBox');
var sVOption = document.getElementById("dropdown_serverVirtualisation");
var sV_Value = 10;
var dVOption = document.getElementById("dropdown_desktopVirtualisation");
var dV_Value = 20;
// Server Virtualisation Value ----------------------------------------------------------------------------------------- //
if (sVOption.options[sVOption.selectedIndex].value === "N") {sV1 = null; sV2 = 0;}
if (sVOption.options[sVOption.selectedIndex].value === "G") {sV1 = sV_Value * 1; sV2 = sV_Value * 1;}
if (sVOption.options[sVOption.selectedIndex].value === "O") {sV1 = sV_Value * 0.3; sV2 = sV_Value * 1;}
if (sVOption.options[sVOption.selectedIndex].value === "A") {sV1 = sV_Value * 0.5; sV2 = sV_Value * 1}
if (sVOption.options[sVOption.selectedIndex].value === "R") {sV1 = 0; sV2 = sV_Value * 1;}
else if (sVOption.options[sVOption.selectedIndex].value === "U") {sV1 = 0; sV2 = sV_Value * 1;}
// Server Virtualisation Value ----------------------------------------------------------------------------------------- //
// Desktop Virtualisation Value ----------------------------------------------------------------------------------------- //
if (dVOption.options[dVOption.selectedIndex].value === "N") {dV1 = null; dV2 = 0;}
if (dVOption.options[dVOption.selectedIndex].value === "G") {dV1 = dV_Value * 1; dV2 = dV_Value * 1}
if (dVOption.options[dVOption.selectedIndex].value === "O") {dV1 = dV_Value * 0.3; dV2 = dV_Value * 1;}
if (dVOption.options[dVOption.selectedIndex].value === "A") {dV1 = dV_Value * 0.5;dV2 = dV_Value * 1}
if (dVOption.options[dVOption.selectedIndex].value === "R") {dV1 = 0; dV2 = dV_Value * 1;}
else if (dVOption.options[dVOption.selectedIndex].value === "U") {dV1 = 0; dV2 = dV_Value * 1;}
// Desktop Virtualisation Value ----------------------------------------------------------------------------------
var keyTechTotal1 = sV1 + dV1;
var keyTechTotal2 = sV2 + dV2;
var keyTechPercentage = (keyTechTotal1) / (keyTechTotal2) * 100
if (keyTechTotal1 > 0) {keyTechBox.value = keyTechPercentage.toFixed(0) + "%";}
else {keyTechBox.value = 0 + "%";}
}
HTML:
<input class="textBox" runat="server" type="text" id="keyTechTextBox" readonly="true" />
<select class="selectElement" runat="server" id="dropdown_serverVirtualisation" onchange="keyTechAdoption()">
<option value="N">N</option>
<option value="G">G</option>
<option value="O">O</option>
<option value="A">A</option>
<option value="R">R</option>
<option value="U">U</option>
</select>
<select class="selectElement" runat="server" id="dropdown_desktopVirtualisation" onchange="keyTechAdoption()">
<option value="N">N</option>
<option value="G">G</option>
<option value="O">O</option>
<option value="A">A</option>
<option value="R">R</option>
<option value="U">U</option>
</select>
我无法感谢你们这些了不起的人们的帮助和时间:)
答案 0 :(得分:2)
您可以使用自定义属性和更通用的JavaScript来获得所需的可扩展性。
HTML:
<input type="text" id="resultBox" readonly="true" />
<select id="dropdown_test" onchange="calc()">
<option m1="" m2="0" value="N">N</option>
<option m1="1" m2="1" value="G">G</option>
<option m1="0.3" m2="1" value="O">O</option>
<option m1="0.5" m2="1" value="A">A</option>
<option m1="0" m2="1" value="R">R</option>
<option m1="0" m2="1" value="U">U</option>
</select>
的javascript:
function calc() {
var resultBox = document.getElementById('resultBox');
var select1 = document.getElementById("dropdown_test");
var select1_control = 10;
var selected_option = select1.options[select1.selectedIndex];
var m1 = selected_option.getAttribute("m1");
if (m1 == ""){
var1 = null
} else {
var1 = select1_control * parseFloat(m1);
}
var m2 = selected_option.getAttribute("m2");
if (m2 == ""){
var2 = null
} else {
var2 = select1_control * parseFloat(m2);
}
resultBox.value = (var1 + var2);
}
答案 1 :(得分:2)
这就是为什么我喜欢jquery ... 30行代码= 6行..:)
使用HTML5数据属性和jquery ..只需要在数据属性中添加你的值。
HTML
<input type="text" id="resultBox" readonly="true" />
<select id="dropdown_test" >
<option value="N" data-one="0" data-two="0">N</option>
<option value="G" data-one="1" data-two="1">G</option>
<option value="O" data-one="0.3" data-two="1">O</option>
<option value="A" data-one="0.5" data-two="1">A</option>
<option value="R" data-one="0" data-two="1">R</option>
<option value="U" data-one="0" data-two="1">U</option>
</select>
jquery的
$(function(){
$('#dropdown_test').change(function(){
var select1_control = 10;
var str= select1_control * $(this).find('option:selected').data('one') + select1_control * $(this).find('option:selected').data('two');
$('#resultBox').val(str);
});
});
<强>更新强>
像我说的那样......只需要在选择框中添加数据属性..<强> HTML 强>
<input class="textBox" runat="server" type="text" id="keyTechTextBox" readonly="true" />
<select class="selectElement" runat="server" id="dropdown_serverVirtualisation">
<option value="N" data-one="0" data-two="0">N</option>
<option value="G" data-one="1" data-two="1">G</option>
<option value="O" data-one="0.3" data-two="1">O</option>
<option value="A" data-one="0.5" data-two="1">A</option>
<option value="R" data-one="0" data-two="1">R</option>
<option value="U" data-one="0" data-two="1">U</option>
</select>
<select class="selectElement" runat="server" id="dropdown_desktopVirtualisation">
<option value="N" data-one="0" data-two="0">N</option>
<option value="G" data-one="1" data-two="1">G</option>
<option value="O" data-one="0.3" data-two="1">O</option>
<option value="A" data-one="0.5" data-two="1">A</option>
<option value="R" data-one="0" data-two="1">R</option>
<option value="U" data-one="0" data-two="1">U</option>
</select>
<强> jquery的强>
$(function(){
$('.selectElement').change(function () {
var sV_Value = 10;
var dV_Value = 20;
var sV1 = sV_Value * $('#dropdown_serverVirtualisation').find('option:selected').data('one')
var sV2 = sV_Value * $('#dropdown_serverVirtualisation').find('option:selected').data('two');
var dV1 = dV_Value * $('#dropdown_desktopVirtualisation').find('option:selected').data('one')
var dV2 = dV_Value * $('#dropdown_desktopVirtualisation').find('option:selected').data('two');
var keyTechTotal1 = sV1 + dV1;
var keyTechTotal2 = sV2 + dV2;
var keyTechPercentage = (keyTechTotal1) / (keyTechTotal2) * 100
if (keyTechTotal1 > 0) {
var str = keyTechPercentage.toFixed(0) + "%";
} else {
var str = 0 + "%";
}
$('#keyTechTextBox').val(str);
});
$('.selectElement').trigger('change'); //<---this if you need the % in page load to when N and N is selected
});
没有改变你的大部分计算部分......
答案 2 :(得分:1)
<强> HTML 强>:
<select id="dropdown_test" onchange="calc()">
<option value="N" data-var1="null" data-var2="0">N</option>
<option value="G" data-var1="1" data-var2="1">G</option>
<option value="O" data-var1="0.3" data-var2="1">O</option>
<option value="A" data-var1="0.5" data-var2="1">A</option>
<option value="R" data-var1="0" data-var2="1">R</option>
<option value="U" data-var1="0" data-var2="1">U</option>
</select>
<强>的Javascript 强>:
function calc() {
var resultBox = document.getElementById('resultBox');
var select1 = document.getElementById("dropdown_test");
var item = select1.options[select1.selectedIndex];
var select1_control = 10;
var var1 = item.getAttribute('data-var1');
var var2 = item.getAttribute('data-var2');
resultBox.value = parseFloat(var1) + (parseFloat(var2) * select1_control);
}
答案 3 :(得分:1)
如果您可以更改HTML,我只需添加一些数据属性即可使其更加简单。例如:
<input type="text" id="resultBox" readonly="true" />
<select id="dropdown_test" onchange="calc()">
<option data-val1="0" data-val2="0" value="N">N</option>
<option data-val1="1" data-val2="1" value="G">G</option>
<option data-val1="0.3" data-val2="1" value="O">O</option>
<option data-val1="0.5" data-val2="1" value="A">A</option>
<option data-val1="0" data-val2="1" value="R">R</option>
<option data-val1="0" data-val2="1" value="U">U</option>
</select>
然后,您只需使用所选选项的值来计算总数:
function calc() {
var select1_control = 10;
var val1 = parseFloat(jQuery('#dropdown_test').find(':selected').data('val1'), 10);
var val2 = parseFloat(jQuery('#dropdown_test').find(':selected').data('val2'), 10);
jQuery('#resultBox').val((val1 * select1_control) + (val2 * select1_control));
}
答案 4 :(得分:1)
如果不更改HTML,您可以将javascript重构为:
function calc() {
var SELECT1_CONTROL = 10;
var matrix = {
"N": [null, 0],
"G": [1, 1],
"O": [0.3, 1],
"A": [0.5, 1],
"R": [0, 1],
"U": [0, 1]
}
var selectedValue = document.getElementById("dropdown_test").value;
var var1 = SELECT1_CONTROL * matrix[selectedValue][0];
var var2 = SELECT1_CONTROL * matrix[selectedValue][1];
var resultBox = document.getElementById('resultBox');
resultBox.value = (var1 + var2);
}
或考虑按照里卡多的建议进入数据注释路线