在表单内提交按钮,而无需重新加载表单

时间:2020-02-22 07:48:50

标签: javascript html

我有一个html表单,其中包含以下字段来创建配方:

<form class="login-form" action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
    <div class="form-group <?php echo (!empty($name_err)) ? 'has-error' : ''; ?>">
        <label>Name</label>
        <input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
        <span class="help-block"><?php echo $name_err; ?></span>
    </div>
    <div class="form-group">
        <label>Image</label>
        <textarea name="image" class="form-control"><?php echo $image; ?></textarea>
        <span class="help-block"></span>
    </div>
    <div class="form-group">
        <label>Video Name</label>
        <input type="text" name="video_name" class="form-control" value="<?php echo $video_name; ?>">
        <span class="help-block"></span>
    </div>
    <div id="ingredients" class="form-group <?php echo (!empty($ingredient_err)) ? 'has-error' : ''; ?>">
        <label>Ingredient Name</label>
        <input type="text" name="ingredient_name" class="form-control" value="<?php echo $ingredient_name; ?>">
        <label>Measure</label>
        <input type="text" name="ingredient_measure" class="form-control" value="<?php echo $ingredient_measure; ?>">
        <label>Unit</label>
        <input type="text" name="ingredient_unit" class="form-control" value="<?php echo $ingredient_unit; ?>">
        <button type="submit" class="btn api-button" onClick="addIngredient()"> Add Ingredient</button>
    </div>
    <div id="addedIngredient"></div>
    <div class="form-group <?php echo (!empty($step_err)) ? 'has-error' : ''; ?>">
        <label>Steps</label>
        <textarea name="steps" class="form-control"></textarea>
        <span class="help-block"><?php echo $steps_err; ?></span>
    </div>
    <div class="form-group <?php echo (!empty($rating_err)) ? 'has-error' : ''; ?>">
        <label>Rating</label>
        <select id="rating" name="rating">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select>
        <span class="help-block"><?php echo $rating_err; ?></span>
    </div>
    <div class="form-group <?php echo (!empty($servings_err)) ? 'has-error' : ''; ?>">
        <label>Servings</label>
        <textarea name="servings" class="form-control"><?php echo $servings; ?></textarea>
        <span class="help-block"><?php echo $servings_err; ?></span>
    </div>
    <div class="form-group <?php echo (!empty($difficultyID_err)) ? 'has-error' : ''; ?>">
        <label>Difficulty</label>
        <select id="difficulty" name="difficulty">
            <option value="1">Easy</option>
            <option value="2">Medium</option>
            <option value="3">Hard</option>
        </select>
        <span class="help-block"><?php echo $difficultyID_err; ?></span>
    </div>
    <div class="form-group <?php echo (!empty($maxTime_err)) ? 'has-error' : ''; ?>">
        <label>Max Time</label>
        <input type="text" name="maxTime" class="form-control" value="<?php echo $time; ?>">
        <span class="help-block"><?php echo $maxTime_err; ?></span>
    </div>
    <input type="hidden" name="recipe_ID" value="<?php echo $id; ?>"/>
    <input type="submit" class="btn btn-primary" value="Submit">
    <a href="show-all-recipes.php" class="btn btn-default">Cancel</a>
</form>

在成分字段之后,我有一个按钮,理想情况下会添加一个新的输入字段,以将另一个成分添加到包含配方成分的表中。此刻,该按钮正确调用了该函数,并且该函数按其应有的方式运行,但似乎还提交了重新加载页面的表单,并且输入字段消失了。有没有一种方法可以只提交此按钮以添加字段而不提交表单,或者我应该在此按钮之间创建两个表单,使其没有任何形式?

我的功能:

function addIngredient() {
    var area = document.getElementById("addedIngredient");
    var input = document.createElement("INPUT");
    area.appendChild(input);
}

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:3)

将“添加成分”的按钮类型更改为“按钮”,您的表格将相应地工作。当前,您有一个按钮类型“提交”,它也提交了表单。

请尝试这个。希望这会有所帮助。

相关问题