有两个问题:
1、我在使用jquery的click事件时,发现有的时候会失灵。就是点击按钮(submit类型)却不执行click事件,这种现象偶尔会发生,可能是什么原因引起的呢?
2、使用click事件提交表单时,表单只能提交一次。具体地说就是:第一次点击按钮,可以提交表单,但是修改表单数据后,再次点提交,虽然执行了click事件,但是没有执行提交表单的操作。这个又是为什么呢?
请各位大神多多指教,谢谢!

解决方案 »

  1.   


    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/JavaScript">
    /*对于你说的第一个问题,是不是有可能是你直接就写下
    $("#abv").click(function(),而没有加上前边的$(function()
    你可以屏蔽掉代码的第一行和最后一行试一下结果*/
    $(function(){
    $("#abv").click(function(){
    alert(123);
    });
    })
    </script>
    <input type="submit" id="abv" value="1234"/>对于你说的第二个问题,我建议你把submit按钮绑定
    onclick事件

    <script type="text/JavaScript">
    function do_check(){
    alert(456);
    }
    </script>
    <input type="submit" id="abv" value="123" onclick="do_check();"/>
      

  2.   

    建议 input 就是button  提交表单 submit 写在 click中
      

  3.   

    第一个问题,我是写在$(document).read(function(){})里边的。每次第一次点击都不行,点击后重新打开页面再点击就可以
    第二个问题,我试过了,用onclick还是不行,这个表单有个特点就是有多个提交键,也就是一个表单的数据有多种处理。如果只有一种处理方式,将表单提交直接放在$(document).read(function(){})一切正常,提交方式写在.click函数中或者是onclick函数中就会出现只能提交一次的问题
      

  4.   

    代码比较长,简化一下,是这样的:
    第一个问题:
    按钮:
    <input type = "submit" name =  "chart" id = "chart" value = "生成图表"/>
    处理:
    $("#chart").click(function(){
    alert("in");
        var options = { 
            //target:        '#output2',   // target element(s) to be updated with server response 
            beforeSubmit:  getData,  // pre-submit callback 
            success:       showChart  // post-submit callback 
        }; 
        
        $('#analyseForm').ajaxForm(options); 
        
        $('#analyseForm').submit(function() { 
        
            $(this).ajaxSubmit(); 
     
            return false; 
        }); 
    });
    第二个问题:
    按钮:(一个表单三个提交按钮)
    <input type="submit" id = "search" value="查询" />
    <input type = "submit" id = "chart" value = "生成图表"/>
    <input type = "button" id = "generReport" value = "生成统计报表"/>
    处理:
    $(document).ready(function() { 
    //查询
     $("#search").click(function(){ 
    var options = { 
            //target:        '#output2',   // target element(s) to be updated with server response 
            beforeSubmit:  showRequest,  // pre-submit callback 
            success:       showResponse  // post-submit callback 
        }; 
      // bind form using 'ajaxForm' 
        $('#demoForm').ajaxForm(options); 
        
        // bind to the form's submit event 
        $('#demoForm').submit(function() { 
            // inside event callbacks 'this' is the DOM element so we first 
            // wrap it in a jQuery object and then invoke ajaxSubmit 
            $(this).ajaxSubmit(); 
     
            // !!! Important !!! 
            // always return false to prevent standard browser submit and page navigation 
            return false; 
        }); 
    }); 
    //生成分析报表
    var start = null;
    var end = null;
    $("#generReport").click(function(){
    start = $("#stringStartTime").val();
    end = $("stringEndTime").val();
    $("#demoForm").attr("action", "dataAnalyse.do?tabName=LogJjb");
    $("#demoForm").submit(function(){
    if (start == "" || end == ""){
    alert("请输入起止时间");
    return false;
    }else{
    return true;
    }
    });  
    });//生成图表
    $("#chart").click(function(){
    alert("in");
        var options = { 
            //target:        '#output2',   // target element(s) to be updated with server response 
            beforeSubmit:  getData,  // pre-submit callback 
            success:       showChart  // post-submit callback 
        }; 
        $('#demoForm').ajaxForm(options); 
        
        $('#demoForm').submit(function() { 
            $(this).ajaxSubmit(); 
     
            return false; 
        }); 
    });
    }); 
    提交前后的处理函数就不贴了,因为提交都不执行,提交前后的处理也就无效了,而且第一次提交一切正常。
      

  5.   

    已经用到了ajaxForm的提交方式, 还需要搞submit类型的按钮吗?全部用type='button'吧
      

  6.   

    我原来也是这么理解的,但是我发现,改成button后,就不会提交表单了,不知道是为什么
      

  7.   

    如果 是 type=“submit” 提交 你想控制 submit 提交 要在form 中控制 符代码 你自己看看把<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function()
        { jQuery('.onclinks').click(function test(){
    var testvaule =jQuery('.text1').val();
    if(testvaule == 1){

    return true;

    }else{

    alert('输入1的时候才能提交');

    return false;

    }
    })

    })
    </script></head><body><form class="form1" action="http://www.opcnz.com" onsubmit="return test()">
    <input type="text" name="text" class="text1" value="">
    <input type="submit" class="onclinks" name="submit" value="提交">
    </form>
    </body>
    </html>
      

  8.   

    解决了,原先使用的jquery Form的ajax提交,使用的绑定options的方法用提交的,后来改成jquery ajax提交表单,即改成了
    $.ajax({
    ……
    });
    然后把按钮改成button
    但是不知道为什么会这样,表单提交的两种ajax方法有什么不同,求大神解惑
      

  9.   


    jquery form , 不过是jquery 的插件, 并不纯正。
    而且很有可能 此插件对jquery 的版本有要求。两者如不匹配就可能出错。所以直接用jquery的ajax提交是最好。
      

  10.   

    多谢了,但是第一个问题,还么解决呢,button 的click方法不被响应是为什么呢?不是不提交表单,而是连那个警告框都不弹出来,用ie开发者工具看了一下,点击buton的时候,根本就没有调用click函数是为什么呢?
      

  11.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
    <title>无标题文档</title>
    <script type="text/javascript">
        //一定得放在页面加载完毕之后。否则按钮对象都没有加载,定义是无效的。
        //或者把js写在页面尾部
        $(function () {
            $("#chart").click(function () {
                alert("in");
            });
        });
    </script>
    </head>
    <body>
    <form id="analyseForm" >
    <input type = "button" name =  "chart" id = "chart" value = "生成图表"/>
    </form>
    </body>
    </html>