Combine 3 functions into one in javascript

后端 未结 2 445
心在旅途
心在旅途 2021-01-29 13:43

I tried to combine these 3 functions into one, but after I did that, it won`t work. can you help me combine those?

function showForm(id, name) {         


        
相关标签:
2条回答
  • 2021-01-29 13:46

    Could you try this one?

    var functionCollections = {
    showForm : function(id, name) {
        document.getElementById('submitForm').style.display = "block";
        if (id == 0) {
            document.getElementById('submitForm').style.display = "none";
        } else {
            document.getElementById(i).style.display = 'block';
        }
    },
    TestForm : function(id, name) {
    document.getElementById('TestFormSubmit').style.display = "block";
        if (id == 0) {
            document.getElementById('TestFormSubmit').style.display = "none";
        } else {
            document.getElementById(i).style.display = 'block';
        }
    },
    FormOne : function(id, name) {
        document.getElementById('FormOneSubmit').style.display = "block";
        if (id == 0) {
            document.getElementById('FormOneSubmit').style.display = "none";
        } else {
            document.getElementById(i).style.display = 'block';
        }
    }
    

    };

    and on the html instead using showForm(id, name); use functionCollections.showForm(id, name);

    0 讨论(0)
  • 2021-01-29 13:48

    Try This :

    function showForm (id, name)  { 
        document.getElementById('submitForm').style.display = "block";
        if (id == 0) 
            document.getElementById('submitForm').style.display = "none";
         else 
            document.getElementById(i).style.display = 'block';
        }
    

    JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.

    0 讨论(0)
提交回复
热议问题