简体   繁体   English

jquery/javascript: 数组

[英]jquery/javascript: arrays

I am a begginer with Javascript/jQuery and I hope someone can help me with the following:我是 Javascript/jQuery 的初学者,希望有人可以帮助我解决以下问题:

I have a simple form (7 questions; 3 radio buttons/answers per question - except for question 5 with 8 possible choices ) and based on the selected answers, when user clicks on 'view-advice' I want to display relevant advices (combination of 38 possible advices) below the form.我有一个简单的表格(7 个问题;每个问题 3 个单选按钮/答案 - 除了第 5 个问题,有 8 个可能的选择)并根据所选答案,当用户点击“查看建议”时,我想显示相关建议(组合38 条可能的建议)在表格下方。 I have given "a", "b", "c",... values to radio buttons and I am collecting them in an array.我已经为单选按钮提供了“a”、“b”、“c”、... 值,并将它们收集在一个数组中。 The part where the script alerts the array works ok.脚本警告数组的部分工作正常。 I can't figure out the part where I display the advices depending on the values in the array.我不知道根据数组中的值显示建议的部分。

I'd appreciate your help!感谢您的帮助! Thanks!谢谢!

Here is the code:这是代码:


var laArray = new Array();

$('.button-show-advice').click(function(){

    $(":radio:checked").each(function(i){
        laArray[i] = $(this).val();
        if (laArray == ["a","d","g","j","m","u"]) {
        $("#advice-container, #advice1, #advice2").show(); // something is wrong here :(
            };
    })
    alert(laArray) // testing to see if it works

})

Rather than test for equality, I think the better means is to check whether or not each of your values are in the array using the jQuery inArray function.与其测试是否相等,我认为更好的方法是使用jQuery inArray函数检查每个值是否在数组中。

Granted, this is just the beginning of code.当然,这只是代码的开始。 You could probably write a function to shore this up, like so.您可能可以编写一个函数来支持这一点,就像这样。

function radioSelected(val) {
  return ($.inArray(val, laArray) != -1);
}

and adapt it to your existing script.并将其调整为您现有的脚本。

You cannot compare arrays this way you should probably either compare each element of the 2 arrays您不能以这种方式比较数组,您可能应该比较两个数组的每个元素

function compare_array(array1,array2) {
    var i;
    for(i=0;i=array1.length;i++) {
        if(array1[i]==array2[i]) {
            return false;
        }
    }
    return true;
}

or serialize the array in a comparable form ( comma separated string for example )或以可比较的形式序列化数组(例如逗号分隔的字符串)

function compare_array(array1,array2) {
     return array1.join(",")==array2.join(",");
}

Would be nice to see the HTML code.很高兴看到 HTML 代码。 But I guess you want to do something like this:但我想你想做这样的事情:


var laArray = [];
var compareValues = function(arr1, arr2) {
  $(arr1).each(function(index, el) {
   if(el !== arr2[index]) {
     return false;
   }
  });
  return true;
};

$('.button-show-advice').click(function(){
    $(":radio:checked").each(function(i){
        laArray.push($(this).val());        
    });
   if(compareValues(laArray,["a","d","g","j","m","u"])) {
      $("#advice-container, #advice1, #advice2").show();
   }  
});

EDIT: updated the code, forgot the });编辑:更新了代码,忘记了 }); ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM