简体   繁体   English

在按钮上显示div上显示div

[英]Show div on button click javascript

I have a div which is invisible by default. 我有一个默认情况下不可见的div。 I want, when button click it shows up. 我想要,当按钮单击显示。
I have tried, but the problem is it shows for just seconds and then again hide. 我已经尝试过了,但问题是它只显示了几秒钟,然后再次隐藏。

Here is my code: 这是我的代码:

function validate() {
    var ta = document.getElementById("t").value;
    var oa = document.getElementById("oa").value;
    var ob = document.getElementById("ob").value;
    var oc = document.getElementById("oc").value;
    var od = document.getElementById("od").value;


    if (ta == "") {
        alert("Title can't be null");
        document.getElementById("t").style.borderColor = "#E34234";
        return false;
    }

    if (oa == ""  &&  ob == "") {
        alert("Atleast two options are compulsory");
        document.getElementById("oa").style.borderColor = "#E34234";
        document.getElementById("ob").style.borderColor = "#E34234";
        return false;
    }

    document.getElementById("g").style.visibility="visible";
    return true;

}

Div id is 'g' and on submit button function validate() is called which validates the form and also show the div. Div id为'g'并在提交按钮上function validate()表单并显示div。

Just return false instead of true. 只是return false而不是true。 It will stop page refresh and the div won't be hidden. 它将停止页面刷新,并且不会隐藏div。 Also, if you need the page refresh, just pass a GET parameter with the url and when the page is loaded, check the get parameter and if its set, make the div visible by default. 另外,如果需要刷新页面,只需传递带有url的GET参数,并在加载页面时检查get参数,如果设置了该参数,则默认情况下使div可见。

function validate() {
    var ta = document.getElementById("t").value;
    var oa = document.getElementById("oa").value;
    var ob = document.getElementById("ob").value;
    var oc = document.getElementById("oc").value;
    var od = document.getElementById("od").value;


if (ta == "") {
    alert("Title can't be null");
    document.getElementById("t").style.borderColor = "#E34234";
    document.getElementById("g").style.visibility="visible";

    return false;
}

if (oa == ""  &&  ob == "") {
    alert("Atleast two options are compulsory");
    document.getElementById("oa").style.borderColor = "#E34234";
    document.getElementById("ob").style.borderColor = "#E34234";
    document.getElementById("g").style.visibility="visible";
    return false;
}

return true;
}

This way, the div will be shown only if the validation has failed. 这样,仅当验证失败时才显示div。 If you want to submit the form as well as keep the div visible, you need to use the approach with get Parameter, or you need to use ajax. 如果要提交表单并保持div可见,则需要将方法与get Parameter一起使用,或者需要使用ajax。

I'm taking a guess here and assuming that the form is submitting and hence you see the div being visible for a fraction of a second. 我在这里进行猜测,并假设表单正在提交,因此您会看到div可见一秒钟。 You should use this code instead: 您应该改用以下代码:

function validate() {
    var ta = document.getElementById("t").value;
    var oa = document.getElementById("oa").value;
    var ob = document.getElementById("ob").value;
    var oc = document.getElementById("oc").value;
    var od = document.getElementById("od").value;

    var flag = false; // initially assume that all is well


    if (ta == "") {
        alert("Title can't be null");
        document.getElementById("t").style.borderColor = "#E34234";
        flag = true; // something wrong, flag it
    }

    if (oa == ""  &&  ob == "") {
        alert("Atleast two options are compulsory");
        document.getElementById("oa").style.borderColor = "#E34234";
        document.getElementById("ob").style.borderColor = "#E34234";
        flag = true; // something wrong, flag it
    }

    if(flag) // if something wrong, show div and disable form submit
    {
       document.getElementById("g").style.visibility="visible";
       return false;
    }

    return true;

}

What we are doing here is creating a flag to check its value at the end. 我们在这里所做的是创建一个标志,以最后检查其值。 If it's true , it means there are errors on form and hence form submit should be disabled. 如果为true ,则表示表单上存在错误,因此应禁用表单提交。 If not, then there are no errors and form submit can proceed as usual. 如果没有,那么就没有错误,并且表单提交可以照常进行。

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

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