提交和onclick不能一起工作 [英] Submit and onclick not working together

查看:107
本文介绍了提交和onclick不能一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < input type =submitclass =subonClick =exefunction2()id =button> 

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

解决方案



 < form onsubmit =return exefunction2 

最好的方法是在窗体标签上调用你的方法。 ();>
...
< / form>

返回 false 不会提交表单, true 将提交!



发布更多数据(使用exefunction2构建)您可以使用这个

 < script type =text / javascript> 
function exefunction2(form){
//添加动态值
var dynValues = {
val1:1,
val2:bla
};

for(var name in dynValues){
//检查字段是否存在
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]);
}
返回true;
}
< / script>

< form onsubmit =return exefunction2(this);>
< input type =submit/>
< / form>


<input type="submit" class="sub" onClick="exefunction2()" id="button">

When I click on the submit button onClick is not working. How can I fix this? I need to work this on submitting the form because onClick event contains values.

解决方案

The best way is to call you method on the form tag like this:

<form onsubmit="return exefunction2();">
...
</form>

Returning false will not submit the form, true will submit!

to post more data (builded with exefunction2) you can use this

<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>

这篇关于提交和onclick不能一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆