数组和的Jquery结果不正确 var answ=[]; $(文档).ready(函数(){ $(“#结果”)。单击(函数(){ answ=[]; $('input[type=“radio”]:checked')。每个(函数(){ answ.push($(this.val());//在数组中推送值 }); var合计=0; 对于(变量i=0;i

数组和的Jquery结果不正确 var answ=[]; $(文档).ready(函数(){ $(“#结果”)。单击(函数(){ answ=[]; $('input[type=“radio”]:checked')。每个(函数(){ answ.push($(this.val());//在数组中推送值 }); var合计=0; 对于(变量i=0;i,jquery,Jquery,您可能需要将“total”变量声明为全局变量。在代码顶部添加以下行: var answ = []; $(document).ready(function() { $("#result").click(function() { answ = []; $('input[type="radio"]:checked').each(function(){ answ.push($(this).val()); //push values in array }); var total = 0; for (

您可能需要将“total”变量声明为全局变量。在代码顶部添加以下行:

var answ = [];

$(document).ready(function() {
$("#result").click(function() {
answ = [];
$('input[type="radio"]:checked').each(function(){

answ.push($(this).val());  //push values in array
});
var total = 0;
for (var i = 0; i < answ.length; i++) {
total += answ[i] << 0;
}
console.log(total);
});
});

$("#shlong").click(function() {
console.log(answ);
});


 FB.ui(
 {
  method: 'feed',
  name: 'Facebook Dialogs',
  link: 'http://developers.facebook.com/docs/reference/dialogs/',
  picture: 'http://fbrell.com/f8.jpg',
  caption: total,
  description: 'Dialogs provide a simple, consistent interface for                     applications to interface with users.',  
   message: 'Facebook Dialogs are easy!'
   },

 <input type="radio"  name="quiz1" value="1">
 <input type="radio"  name="quiz2" value="4">
 <input type="radio"  name="quiz3" value="1">
 <button id="result">penis</button>
 <input type="button" onclick="share_prompt()" value="Share" />

我将为您提供一种更简单的方法,而不是去除杂乱无章的未缩进代码。首先,我问了自己一个无法找到好答案的问题

为什么要在数组中存储每个值?? 好了,我的系统现在没有了。让我们避免使用你的无用数组,快速完成任务

我的建议是在循环时将每个值添加到
总数中。。
大概是这样的:

var total = 0;
$(文档).ready(函数(){
$(“#结果”)。单击(函数(){
var合计=0;
$('input[type=“radio”]:checked')。每个(函数(){

total+=this.value我不认为当用户界面拾取它时,它只是出于某种原因将其显示为一个数组。复选框呢?请也发布html。
将数组添加到一起的预期结果是什么total就是结果。我想你不知道它真正的作用。。这是一个称为左移位的按位运算符,或者其他什么像那样..就像左边的数字乘以2^n,右边的数字是n。在你的例子中,
n=0
所以它就像乘以1。换句话说,转换为
int
。对不起,我是个傻瓜,我真的应该再查一下我猜的基础知识
$(document).ready(function () {
    $("#result").click(function () {
        var total = 0;

        $('input[type="radio"]:checked').each(function () {
            total += this.value << 0;
        });

        alert(total);
    });
});