简体   繁体   English

Javascript上的程序无法正常工作

[英]program on Javascript doesn't work correctly

I've a problem with result of this program: when I checked "Protector", "Headset", "Case" and "Games" as a result I should receive "Kindle", "Nokia" and "Wii". 我对该程序的结果有疑问:当我检查“ Protector”,“ Headset”,“ Case”和“ Games”时,应该收到“ Kindle”,“ Nokia”和“ Wii”。 However, I receive only one value - "Kindle" (this is in Google Chrome and Internet Explorer). 但是,我仅收到一个值-“ Kindle”(在Google Chrome和Internet Explorer中)。 In addition, I receive all the values (duplicated also) this is in Opera. 另外,我收到Opera中所有的值(也重复)。 Can you help me to resolve these problems. 您能帮我解决这些问题吗? Thanks in advance. 提前致谢。

<html>
<head>
<script type="text/javascript">

function product(){ 
    var checkboxes = document.getElementsByName('checkboxes');
    var i=0;
    var j=0;
    var tech = [
    ["Kindle","Protector"],
    ["Iphone","Headset"],
    ["Iphone","US Charger"],
    ["Iphone","USB cable"],
    ["Iphone","Case"],
    ["Nokia","Protector"],
    ["Nokia","Headset"],
    ["Nokia","Case"],
    ["Wii","Games"]
    ];

    for (i=0 ; i < tech.length; i++){
        for(j=0 ; j< checkboxes.length; j++){
            if ((tech[i][1] == checkboxes[j].value)&&(checkboxes[j].checked)){
            document.write(tech[i][0]);
            document.write("<br>");
            }
        }
    }
}
</script>
</head>

<body>

<form name="Accessories">
<input type="checkbox" name="checkboxes" value="Protector"> Protector <br>
<input type="checkbox" name="checkboxes" value="Headset"> Headset <br>
<input type="checkbox" name="checkboxes" value="US Charger"> US Charger <br>
<input type="checkbox" name="checkboxes" value="USB cable"> USB cable <br>
<input type="checkbox" name="checkboxes" value="Case"> Case <br>
<input type="checkbox" name="checkboxes" value="Games"> Games </br>
<input type="button" name="Check" onClick="product()" value="Search a product">
</form>

</body>
</html>

Calling document.write() after the page has rendered will obliterate the page completely. 页面呈现后调用document.write()将完全抹掉页面。 In some browsers, that means that the JavaScript code itself will stop working. 在某些浏览器中,这意味着JavaScript代码本身将停止工作。

What you can try instead is this: 您可以尝试的是:

  1. Add a <div> to the end of your page (inside the <body> of course) and give it an "id" value of "settings". 在页面末尾添加一个<div> (当然,在<body>内),并为其赋予一个“ id”值“ settings”。

  2. Change your code to set its contents: 更改代码以设置其内容:

     var content = ""; for (i=0 ; i < tech.length; i++){ for(j=0 ; j< checkboxes.length; j++){ if ((tech[i][1] == checkboxes[j].value)&&(checkboxes[j].checked)){ content += tech[i][0] + '<br>'; } } } document.getElementById("settings").innerHTML = content; 

The moment you use document.write() your form elements exist no more so your checkboxes array will not be present. 当您使用document.write()时,表单元素将不复存在,因此您的checkboxes数组将不存在。 That is the reason why you are getting just 'kindle'. 这就是为什么您只获得“亲情”的原因。 Try to use console.log(tech[i][0]) instead and run it in chrome. 尝试改用console.log(tech [i] [0])并在chrome中运行它。 You should get: Kindle, Iphone, Iphone, Nokia, Nokia, Nokia, Wii. 您应该得到:Kindle,Iphone,Iphone,诺基亚,诺基亚,诺基亚,Wii。

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

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