如何用或和条件评价表达方式?

时间:2017-01-10 05:41:17

标签: javascript jquery html

通过检查单选按钮获得三个值,获取我在我的条件下使用的值。我试过了

<html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
    </head>
    <body>
        <form>
            <p>question 1</p>
            <p><input type="radio" name="q1" value="A"><label>Agree</label><input type="radio" name="q1" value="N"><label>Nuetral</label>
            <input type="radio" name="q1" value="D"><label>DisAgree</label><input type="radio" name="q1" value="NA"><label>NOt Applicable</label></p>
            <p>question 2</p>
            <p><input type="radio" name="q2" value="A"><label>Agree</label><input type="radio" name="q2" value="N"><label>Nuetral</label>
            <input type="radio" name="q2" value="D"><label>DisAgree</label><input type="radio" name="q2" value="NA"><label>NOt Applicable</label></p>
            <p>question 3</p>
            <p><input type="radio" name="q3" value="A"><label>Agree</label><input type="radio" name="q3" value="N"><label>Nuetral</label>
            <input type="radio" name="q3" value="D"><label>DisAgree</label><input type="radio" name="q3" value="NA"><label>NOt Applicable</label></p>
            <button id="submit">Submit</button>
        </form>
            <script>
            $(document).ready(function(){
                var A = 10, N = 6, D = 3, NA = 0;
                var v1, v2, v3;
                $("#submit").click(function(){debugger;
                var q1 = $('input[name=q1]:checked').val();
                var q2 = $('input[name=q2]:checked').val();
                var q3 = $('input[name=q3]:checked').val();     
                 toString1(q1v, q2v, q3v);              
                    if((q1 == "A" || q1 == "N") && ((q1+q2+q3)/3 > 0.60)){
                     console("q1 Agree or Nuetral and also value is above 60%");
                    }
                    else if((q2 == "A" || q2 =="N") && ((q1+q1+q1/3) > 0.60)){
                     console("q2 Agree or Nuetral and also value is above 60% ");
                    }
                    else if((q3 == "A" || q3 == "N") && ((q1+q1+q1/3) > 0.60)){
                     console("q3 Agree or Nuetral and also value is above 60% ");
                    }
                    else{
                        console("q1,q2 and q3 DisAgree or NotApplicable and also value is bellow 60% ");
                    }
                });
            });
            </script>
        </body>
</html> 

1 个答案:

答案 0 :(得分:1)

您可以首先声明一个对象,其中键是您从表单中检索的代码,值是数值,如下所示:

var values = {
  'A': 10,
  'N': 6,
  'D': 3,
  'NA': 0
}

现在,您可以values[q1]访问您的值,其中q1是A,N,D或NA之一。这是一个有效的例子:

$(function () {
  var values = {
    'A': 10,
    'N': 6,
    'D': 3,
    'NA': 0
  }

  $('#submit').on('click', function () {
    var q1 = $('input[name=q1]:checked').val()
    var q2 = $('input[name=q2]:checked').val()
    var q3 = $('input[name=q3]:checked').val()

    console.log('Value for q1: ' + values[q1])
    console.log('Value for q2: ' + values[q2])
    console.log('Value for q3: ' + values[q3])

    var sum = values[q1] + values[q2] + values[q3]

    console.log(sum)
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <p>question 1</p>
    <p><input type="radio" name="q1" value="A"><label>Agree</label><input type="radio" name="q1" value="N"><label>Nuetral</label>
    <input type="radio" name="q1" value="D"><label>DisAgree</label><input type="radio" name="q1" value="NA"><label>NOt Applicable</label></p>
    <p>question 2</p>
    <p><input type="radio" name="q2" value="A"><label>Agree</label><input type="radio" name="q2" value="N"><label>Nuetral</label>
    <input type="radio" name="q2" value="D"><label>DisAgree</label><input type="radio" name="q2" value="NA"><label>NOt Applicable</label></p>
    <p>question 3</p>
    <p><input type="radio" name="q3" value="A"><label>Agree</label><input type="radio" name="q3" value="N"><label>Nuetral</label>
    <input type="radio" name="q3" value="D"><label>DisAgree</label><input type="radio" name="q3" value="NA"><label>NOt Applicable</label></p>
    <button id="submit">Submit</button>
</form>

PS:尝试全屏查看代码段