提交和onclick不能一起工作

2 html javascript

<input type="submit" class="sub" onClick="exefunction2()" id="button">
Run Code Online (Sandbox Code Playgroud)

当我点击提交按钮onClick不起作用.我怎样才能解决这个问题?我需要在提交表单时这样做,因为onClick事件包含值.

sil*_*lly 11

最好的方法是在表单标记上调用方法,如下所示:

<form onsubmit="return exefunction2();">
...
</form>
Run Code Online (Sandbox Code Playgroud)

退货false不会提交表格,true会提交!

要发布更多数据(使用exefunction2构建),您可以使用它

<script type="text/javascript">
    function exefunction2(form) {
        //add dynamic values
        var dynValues = {
            "val1": 1,
            "val2": "bla"
        };

        for(var name in dynValues) {
            //check if field exists
            var input = null;
            if(document.getElementsByName(name).length === 0) {
                input = document.createElement('input');
                input.setAttribute('name', name);
                input.setAttribute('type', 'hidden');
                form.appendChild(input);
            }
            else {
                input = document.getElementsByName(name)[0];
            }

            input.setAttribute('value', dynValues[name]);
        }
        return true;
    }
</script>

<form onsubmit="return exefunction2(this);">
    <input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)