无论用户回答{J},我都会继续确认

时间:2015-07-02 16:29:04

标签: javascript django forms

我有一个与我的Django网站代码一起使用的html模板。在模板中有几种形式。其中两个表单是日期字段,我有一个确认弹出窗口,如果用户填写则会在提交时激活。我希望弹出窗口退出表单提交并返回页面,如果用户点击'取消',并在用户点击'确定'时继续提交表单。

确认弹出窗口正确显示,但无论用户点击什么,表单提交都会继续。我如何更改它,以便在用户点击“取消”时退出表单?

Javascript看起来像这样:

<script type="text/javascript">
    var checkFields = ["SPAN_START","SPAN_END"];
    function checkForm( theForm ) {

      for (i in checkFields ) {
        var fieldName = checkFields[ i ];
        var theField = theForm[ fieldName ];

        if ( theField.value ) {
          var retVal = confirm("Are you sure you want to specify a date?");
          if (retVal == true){
            theField.submit()
            return true;
          } else {
            return false;
          }
        }
      }
    }
</script>

使用它的html如下所示:

<form action="/InterfaceApp/Nominal_Request/" method="post" class="form" onsubmit="checkForm( this )">
  {% csrf_token %}
  <div class="panel-body text-center">
    {% bootstrap_form form_span_start %}
    {% bootstrap_form form_span_end %}
    {% buttons %}
    <button type="submit" class="btn btn-primary center-block" value="Submit" name="Range">
      {% bootstrap_icon "fire" %} Generate Range of Requests
    </button>
    {% endbuttons %}
  </div>
</form>

如果用户点击“取消”,可以告诉我如何取消提交表单吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

您必须将函数的值返回到内联事件处理程序

onsubmit="return checkForm( this )"

最好不要使用内联事件处理程序,而是使用addEventListener