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" />
大家好,我试图将顶部代码($ total)的结果显示在fb.ui中但是它显示整个数组,如:[&#34; 1&#34;],[ &#34; 4&#34;],[&#34; 1&#34;]如何使输出像console.log一样只是单个数字
答案 0 :(得分:2)
我将为您提供一种更简单的方法,而不是对您杂乱的 un-indented 代码进行重新排序。首先,我问自己的问题,并没有找到一个好的答案..
好了,现在它已经退出了我的系统..让我们避免使用你那些无用的数组并快速完成任务......
我的建议是在循环时将每个值添加到total
。
像这样:
$(document).ready(function () {
$("#result").click(function () {
var total = 0;
$('input[type="radio"]:checked').each(function () {
total += this.value << 0;
});
alert(total);
});
});
修改以获得最佳答案,此处为working jsfiddle
答案 1 :(得分:0)
你可能需要声明你的总数&#39;变量作为全局变量。将以下行添加到代码顶部:
var total = 0;