Javascript 复选框和随机列表输出文本而不是变量

标签 javascript html

我试图让复选框页面确定框架中使用的选项。这些列表只是临时供我测试,警报(最终选择)也只是供我查看最后会吐出什么。问题在于,例如 var Finalchoice ='choice0' 而不是 ' http://www.nordstrom.com '。 我觉得这是一个简单的修复,但我无法弄清楚。

function myfucntion(){
var final_list = []     
var checkboxes = document.getElementsByClassName('c');    
for (var i = 0; i < checkboxes.length; i++) {

        final_list = [];
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                final_list.push(['choice'+i]);


            }

    };
}

list0  = ['http://www.nordstrom.com','http://www.nordstom.com'];    
list1 = ['http://www.bing.com','http://www.bing.com'];
list2 = ['http://www.yahoo.com','http://www.yahoo.com'];    
list3 = ['http://www.amazon.com','http://www.amazon.com'];

var choice0  = list1[Math.floor(Math.random()*list1.length)];
var choice1  = list2[Math.floor(Math.random()*list2.length)];
var choice2  = list3[Math.floor(Math.random()*list3.length)];
var choice3  = list4[Math.floor(Math.random()*list4.length)];



var finalchoice = final_list[Math.floor(Math.random()*final_list.length)];
document.getElementById('window').src=finalchoice;
alert(finalchoice)    
}

这是 html:

<div>
<button onclick="myfunction()">myfunction</button>
</div>
<div>
<input type="checkbox" class="c" />
<input type="checkbox" class="c" />
<input type="checkbox" class="c" />
<input type="checkbox" class="c" />
</div>
<iframe style="width:100%;height:500px" id="window" name="window" src="">
</iframe>

最佳答案

for (var i = 0; i < checkboxes.length; i++) {

    final_list = [];
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            final_list.push(['choice'+i]);


        }
    }
}

这个嵌套循环完全是多余的,并且可能是您的复制粘贴错误?

var final_list = []     
var checkboxes = document.getElementsByClassName('c');    
for (var i = 0; i < checkboxes.length; i++) {
    if (checkboxes[i].checked) {
        final_list.push(['choice'+i]);
    }
}

var finalchoice = final_list[Math.floor(Math.random()*final_list.length)];
document.getElementById('window').src=finalchoice;
alert(finalchoice);

The problem is that, for example, var finalchoice ='choice0' rather than 'http://www.nordstrom.com'

final_list 是一个字符串数组,例如 ['choice0', 'choice3', 'choice4'] - 不是变量。

尽管你的问题措辞不好,但我觉得你想要做的是将变量推送到final_list,以便你以后可以修改它们。

function myfunction() {
    list0  = ['http://www.nordstrom.com','http://www.nordstom.com'];    
    list1 = ['http://www.bing.com','http://www.bing.com'];
    list2 = ['http://www.yahoo.com','http://www.yahoo.com'];    
    list3 = ['http://www.amazon.com','http://www.amazon.com'];

    var choice = [];
    choice[0] = list0[Math.floor(Math.random()*list0.length)];
    choice[1] = list1[Math.floor(Math.random()*list1.length)];
    choice[2] = list2[Math.floor(Math.random()*list2.length)];
    choice[3] = list3[Math.floor(Math.random()*list3.length)];

    var final_list = [];
    var checkboxes = document.getElementsByClassName('c');
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            final_list.push(choice[i]);
        }
    }

    var finalchoice = final_list[Math.floor(Math.random()*final_list.length)];
    document.getElementById('window').src=finalchoice;
    alert(finalchoice);
}

编辑:一些对您有利的解释。 首先,您最初尝试将字符串推送到 Final_list 上。字符串不是也不能转换为类似命名的变量(不使用eval(),您可能应该尝试避免)。为了解决这个问题,我们使用带有索引的数组。

其次,我交换了循环和 choice[] 声明 - 当您将变量的值分配给某个对象时,您只是分配了该值的副本。任何 future 的变化都不会改变这一点。

另外,请阅读http://sscce.org/并确保您提供的代码实际上是可编译的。存在许多语法和拼写错误以及一些缩进问题。

关于Javascript 复选框和随机列表输出文本而不是变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21447441/

相关文章:

php - 替换一些文本后保留 HTML 格式(使用 PHP 和 JS)

html - 滑入滑出 html 元素

javascript - 如何在 ajax 调用中将动态 json 文件名作为 url?

javascript - json 字符串化、编码和解码 - 为什么?

javascript - 从 javascript 循环 "when it happens"写入 html

html - 使用表格设计博客?

javascript - 如何确定浏览器是否支持超链接的数据 URI

javascript - Gmail API User.Message 属性不起作用

javascript - 在 Highcharts 中,将小选区放大到边缘,当 Zoomtype = x 时会同时缩放 x 和 y

javascript - 为什么这不起作用(没有 Node 的 React)?