简体   繁体   English

按钮重定向到其他页面

[英]Button redirecting to a different page

JSFiddle Link JSFiddle链接

html html

<body>
    <div class="mainWrapper">
        <div class="newWrapper">

            <form id="newform" method="post" action="new.php">
                <div class="textboxContainer">
                    <input id="question" type="text" name="question" placeholder="Question..." /><br /><br />
                    <input type="text" name="ans1text" placeholder="Answer 1..." value=""/><br />
                    <input type="text" name="ans2text" placeholder="Answer 2..." value=""/><br />
                    <input type="text" name="ans3text" placeholder="Answer 3..." value=""/><br />
                    <input type="text" name="ans4text" placeholder="Answer 4..." value=""/><br />
                </div><br /><br />

                <div class="doubleButtonContainer">
                <button class="moreAnswersButton" href="javascript:void(0)" name="more_answers" onclick="showAllAnswers()">More...</button>
                <input type="submit" class="button" name="submit" value="Finish" /><br />
               </div>

            </form>

        </div>

    </div>
</body>
</html>

jQuery jQuery的

function showAllAnswers()
{
    console.log("WHAT IS GOING ON?????");
    $(".textboxContainer").append("<input type='text' name='ans5text' placeholder='Answer 5...' value=''/><br />");
    $(".textboxContainer").append("<input type='text' name='ans6text' placeholder='Answer 6...' value=''/><br />");
    $(".textboxContainer").append("<input type='text' name='ans7text' placeholder='Answer 7...' value=''/><br />");
    $(".textboxContainer").append("<input type='text' name='ans8text' placeholder='Answer 8...' value=''/><br />");
    $(".moreAnswersButton").remove();
}

For some reason, the <button> is redirecting to another page. 由于某些原因, <button>重定向到另一个页面。 I feel like it might have something to do with it being inside an HTML post form, but I have no idea why it is redirecting when I already have a submit button assigned. 我觉得它可能与它在HTML发布表单中有关,但是我不知道为什么在分配了提交按钮后为什么要重定向。

My end result that I'm looking for is for the More... button to add 4 more text boxes to the form. 我要寻找的最终结果是使用“更多...”按钮向表单添加了另外4个文本框。 I do this html editing with the jquery code. 我用jQuery代码进行html编辑。

How can I fix this issue and make my app work properly? 如何解决此问题并使我的应用正常运行?

If you don't give a <button> tag an explicit "type" attribute, the type defaults to "submit". 如果不为<button>标记提供显式的“类型”属性,则该类型默认为“提交”。 Give it type="button" to override that default. 为其指定type="button"以覆盖该默认值。

A <button> with explicit or implicit type "submit" acts exactly like a "submit" <input> tag. 具有显式或隐式类型“ submit”的<button>行为与“ submit” <input>标记完全相同。

Default button behaviour is to submit. 默认按钮行为是提交。 You need to override that either by setting the button type = "button" or putting return false; 您需要通过设置按钮type = "button"或将return false;来覆盖它return false; after showAllAnswers(); showAllAnswers(); in the onclick event. onclick事件中。

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

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