【问题标题】:Clicking the reset radio button tries to submit the form in Django单击重置单选按钮尝试在 Django 中提交表单
【发布时间】:2015-08-05 23:24:22
【问题描述】:

我在 Django 中创建了一个表单。有一个重置按钮点击它应该重置单选按钮。

<td>
{{form.effort.help_text}}&nbsp;<font size=1 color=red>
    {% for error in form.effort.errors %}
        {{error}}
    {%endfor%}</font>
    {% for choice in form.effort %}
        {{choice.tag}}{{choice.choice_label}}
    {% endfor %}&nbsp;&nbsp;
    <button onclick="resetEffort();">reset</button>
</td>

resetEffort() 函数的代码是 -

<script type=text/javascript>
function resetEffort()
{
    $('input[name="effort"]').prop('checked', false);
}
</script>

但是当我单击此重置按钮时,它会重置单选按钮,但同时尝试提交表单。它显示未填写且为必填字段的错误警告。焦点到达页面顶部。

表单中的保存按钮-

<form action="{% url 'kaizen:addkaizen'%}" method="post">
 <!-- other fields of form here -->
    <input type="submit" value="Save" name="save"/> 

单击重置单选按钮时显示错误(或提交表单时未填写任何值)

如何更正,以便单击重置按钮以静默方式重置单选按钮并且不要尝试提交表单。

PS - 我不想重置整个表单。我只想重置一个字段,即单选按钮组。我已经为整个表单设置了重置按钮。

【问题讨论】:

    标签: javascript jquery html django forms


    【解决方案1】:

    更新:您需要在 &lt;input&gt; 元素中指定 type="button" 以重置表单。更多信息here

    【讨论】:

    • 将按钮的代码从“”更改为“
    • 表单重置按钮完好无损。更改了特定的字段重置按钮。
    【解决方案2】:

    为了防止表单被提交: 将typeattribute 按钮添加到您的按钮,如下所示:

    <button type="button" onclick="resetEffort();">My button</button>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-29
      • 2014-02-25
      • 2022-01-07
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2021-06-15
      相关资源
      最近更新 更多