js 确认表单,点击取消问题

clipboard.png

clipboard.png

点击取消后还是会把表单提交上去
为咋的

表单html

<form  role="search" method="POST">

                <label for="name">选择更新的项目</label>
                    <select  class="form-control" name="selectid" id="optionvalue">
                        {% for key,value in prodict.items() %}
                            <option  value ="{{ key }}" >
                              {{ value }}
                            </option>
                        {% endfor %}
                    </select>
                <div class="form-group" style="margin-top: 40px">
                    {{ versionform.hidden_tag() }}
                    {{ render_field(versionform.version,class="form-control",placeholder="Version Num",id="versionnum") }}
                </div>
                <button type="button" class="btn btn-default" onsubmit="return test();">Submit</button>
            </form>

js内容

function test(){
    var optionNum = document.getElementById("optionvalue").selectedIndex;
    var optionValue = document.getElementById("optionvalue").options;
    var options = optionValue[optionNum].text
    var versionNum = document.getElementById("versionnum").value;
    console.log(options);
    console.log(versionNum);

    if (confirm("更新" + options + "的版本号为:" + versionNum + "?....")){
        return false;
   }else{
        return false;
    }

}

=====搞定了====
放错位置了
html内容

clipboard.png

js内容

clipboard.png

阅读 3.9k
4 个回答

把 onsubmit="return test();" 放到form上面去而不是放到submit的button试试看!

button标签自带submit

  1. 使用 onclick 不要用 onsubmit

  2. 為什麼 test() 無論如何都 return false?

onsubmit="test()"
试试

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题