简体   繁体   English

如何隐藏<span>标签并使用javascript显示</span>

[英]how to hide a <span> tag and show it with javascript

i have this sample html coding 我有这个样本html编码

<form id = "myform" onsubmit = "return checkcheckboxes()">
task 1</task><input type = "checkbox" name = "chk1"></br>
task 2<input type = "checkbox" name = "chk2"></br>
task 3<input type = "checkbox" name = "chk3"></br></br>
<input type = "submit" value = "Go to the next task"></br>
<span id = "message" >congratulations! you have completed all the task completely</span>
</form>

and this is the JS 这是JS

function checkcheckboxes() {
document.getElementById("message").innerHTML = "";
var valid = true;
var f = document.getElementById("myform");
if (f.chk1.checked == false) {valid = false}
if (f.chk2.checked == false) {valid = false}
if (f.chk3.checked == false) {valid = false}
if (!valid) {document.getElementById("message").innerHTML = "You must check all three boxes to proceed"};
return valid;
}

If you open the code in the browser with JS, it will show you three checkboxes with a submit button and there is a span text under submit button saying 如果您使用JS在浏览器中打开代码,它将显示三个带有“提交”按钮的复选框,并且“提交”按钮下方显示一个跨度文本

congratulations! 恭喜你! you have completed all the tasks completely 您已经完全完成了所有任务

If you check >2 boxes and click submit, the text under the submit button will say 如果您选中> 2框并点击提交,则提交按钮下方的文字将显示

You must check all three boxes to proceed 您必须选中所有三个框才能继续

and if you check all 3 boxes the message will change again back to: 如果您选中所有3个框,则消息将再次更改为:

congratulations! 恭喜你! you have completed all the tasks completely 您已经完全完成了所有任务

what i want to do is remove the saying: 我想做的是删除俗语:

congratulations! 恭喜你! you have completed all the tasks completely 您已经完全完成了所有任务

and only show it when all the boxes are checked and submit button is click, since is showing even though boxes are not checked. 并且仅在选中所有框并单击“提交”按钮时显示它,因为即使未选中框也显示它。

What I want to do is remove the saying: "congratulations! you have completed all the tasks completely" and only show it when all the boxes are checked and submit button is click 我想做的是删除谚语: “恭喜!您已经完全完成了所有任务”,并且仅在选中所有复选框并单击提交按钮时显示它

Make the <span> empty: <span id="message"></span> . <span>空: <span id="message"></span> And let the JavaScript fill it when you validate the form. 并在验证表单时让JavaScript填充它。

What if I wanna put a link with the text, should I just use the html anchor tag in the javascript string? 如果我想在文本上放置链接,该怎么办?我应该只在javascript字符串中使用html锚标签吗? EX: congratulations! EX:恭喜! you have completed all the task completely and here is the link 您已经完全完成了所有任务,这是链接

In that case, add the link in the "congratulations..." text and always return false so the form is never submitted. 在这种情况下,请将链接添加到“祝贺...”文本中,并且始终返回false因此永远不会提交表单。

and is there a javascript command that will get all the checkboxes on the page instead of putting each of their name, because I will create a huge checklist 并且有一个javascript命令将获取页面上的所有复选框,而不是放置每个复选框的名称,因为我将创建一个巨大的清单

Yes, there is. 就在这里。 Here's the final code (verifying all checkboxes inside the myform form and adding the anchor tag to the resulting text): 这是最终的代码(验证myform表单内的所有复选框,并将锚标记添加到结果文本中):

function checkcheckboxes() {
    var valid = true;
    var inputs = document.getElementById("myform").getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            if (!inputs[i].checked) {
                valid = false;
                break; // break as soon as it finds an unchecked
            }
        }
    }
    if (!valid) {
        document.getElementById("message").innerHTML = "You must check all boxes to proceed";
    } else {
        document.getElementById("message").innerHTML = "congratulations! you have completed all the tasks completely and <a href='http://www.google.com'>here is the link</a>.";
    }
    return false;
}

See working demo code here . 在这里查看有效的演示代码

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

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