Combine 3 functions into one in javascript

2020-05-09 22:38发布

问题:

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) {
    document.getElementById('submitForm').style.display = "block";
    if (id == 0) {
        document.getElementById('submitForm').style.display = "none";
    } else {
        document.getElementById(i).style.display = 'block';
    }
}

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

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

If it needed, this is HTML, I use JSFiddle, after run it, choose form on right side, then it will shows "download form" but I don`t know how to do that.

<form name="forms" class="form1" id="first">
    <br />
    <p style="text-align: left;" class="form1">Choosing to Make A Difference</p>
    <select id="dropdownMenu" name="budget" onchange="javascript: showForm(document.getElementById('dropdownMenu').value);">
        <option value="0"></option>
        <option value="#">Change Form</option>
        <option value="#">Scholarships and Grants</option>
    </select>
    <br />
    <br />
    <div id="submitForm" style="display: none; position:absolute; margin-top:-20px;">
        <input type="button" id="submit" value="Download Form" onclick="window.open(forms.budget.value,'newtab'+forms.budget.value)" />
    </div>
    <br />
</form>
<form name="forms" class="form1" id="second">
    <br />
    <p style="text-align: left;" class="form1">Choosing to Make A Difference</p>
    <select id="TestFormDropdown" name="budget" onchange="javascript: TestForm(document.getElementById('TestFormDropdown').value);">
        <option value="0"></option>
        <option value="#">Change Form</option>
        <option value="#">Scholarships and Grants</option>
    </select>
    <br />
    <br />
    <div id="TestFormSubmit" style="display: none; position:absolute; margin-top:-20px;">
        <input type="button" id="submit" value="Download Form" onclick="window.open(forms.budget.value,'newtab'+forms.budget.value)" />
    </div>
    <br />
</form>

<form name="forms" class="form1" id="second">
    <br />
    <p style="text-align: left;" class="form1">Choosing to Make A Difference</p>
    <select id="FormOneDropdown" name="budget" onchange="javascript: FormOne(document.getElementById('FormOneDropdown').value);">
        <option value="0"></option>
        <option value="#">Change Form</option>
        <option value="#">Scholarships and Grants</option>
    </select>
    <br />
    <br />
    <div id="FormOneSubmit" style="display: none; position:absolute; margin-top:-20px;">
        <input type="button" id="submit" value="Download Form" onclick="window.open(forms.budget.value,'newtab'+forms.budget.value)" />
    </div>
    <br />
</form>

回答1:

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 {...}.



回答2:

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);