2

非常感谢大家的帮助 - 非常感谢。

但是,我接下来要做的事情真的很复杂,我相信你们有头脑来帮助我。

这是我也想以更好的方式编写的更复杂的代码——更少的代码,更高效。

抱歉,这是很多代码。

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>

我不能感谢你们这些了不起的人的帮助和时间:)

4

5 回答 5

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);
}
于 2013-05-15T18:57:36.263 回答
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
});

没有改变你的大部分计算部分......

更新的小提琴

于 2013-05-15T19:11:12.987 回答
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));
}

小提琴:http: //jsfiddle.net/duffmaster33/z4Pfg/

于 2013-05-15T18:59:07.213 回答
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);
}

演示

于 2013-05-15T18:56:40.823 回答
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);
 }

或考虑按照里卡多的建议走数据注释路线

于 2013-05-15T19:03:23.537 回答