简体   繁体   中英

JavaScript submit button onclick redirect not working

Why this JavaScript code is not redirecting to the URL ? What is wrong with this code?

function submitform() {
    var hiddenFieldValue = document.getElementById('course').value;
    alert("inindonesia.org/marketplace/tags/"+hiddenFieldValue);
    window.location.href= "inindonesia.org/marketplace/tags/"+hiddenFieldValue); 
    return false;
}

HTML code

<form autocomplete="off" method="POST">
        <p>
            Course Name <label>:</label>
            <input type="text" name="course" id="course" />
            <!--input type="button" value="Get Value" /-->
            <input type="text" name="course_val" id="course_val" />
        </p>
        <input type="submit" value="Submit" onClick="submitform()"/>
</form>

As you are relying on button click, what is the need of the form?? As you put that in <form &rt; it is not working. remove form, it will work

        <p>
        Course Name <label>:</label>
        <input type="text" name="course" id="course" />
        <!--input type="button" value="Get Value" /-->
        <input type="text" name="course_val" id="course_val" />
    </p>
    <input type="submit" value="Submit" onClick="submitform()"/>

Remove form.

You need http:// on the URL. You code updated:

function submitform(){
    var hiddenFieldValue = document.getElementById('course').value;
    alert("inindonesia.org/marketplace/tags/"+hiddenFieldValue);
    window.location.href= "http://inindonesia.org/marketplace/tags/"+hiddenFieldValue); 
    return false;
}

You should use only

window.location = url;

or

window.location.replace = url;

instead off

window.location.href

cause that simulates a link

here is the discussion

How to redirect to another webpage in JavaScript/jQuery?

you are doing this :

 window.location.href= "inindonesia.org/marketplace/tags/"+hiddenFieldValue);

change it to this :

 window.location.href= "inindonesia.org/marketplace/tags/"+hiddenFieldValue;

NOTE THE ')' that i deleted at the end of the line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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