2011-04-26 139 views
0

我有一个表单,并有3个文本框提交按钮。在按钮上文本框1应该验证,并且n提交按钮B,其他2应该验证。我使用了提交处理程序并使用ajax提交表单。用两个提交按钮验证表单使用Jquery

我的问题是,如果我点击A然后它没有调用B的提交处理程序如果B被点击。无论首先点击什么,那么另一种方法不起作用。

请帮忙!

我的代码看起来像下面:

function set_new_rate(oid) { 
    alert("set_new_rate"); 
    $("#TestForm").validate({ 
     event: "custom", 
     rules: { 
      client_new_rate: { 
       required: true, 
       number: true 
      }, 
      agent_new_rate: { 
       required: true, 
       number: true 
      } 
     }, 
     messages: { 
      client_new_rate: { 
       required: "Required", 
       number: "Invalid Rate" 
      }, 
      agent_new_rate: { 
       required: "Required", 
       number: "Invalid Rate" 
      } 
     }, 
     submitHandler: function() { 
      var client_new_rate = $('#client_new_rate').val(); 
      var agent_new_rate = $('#agent_new_rate').val(); 

      var answer = confirm("Set New Rate for this Order?") 
      if (answer) 
      { 
       $.post('http://localhost/fc/index.php/disp/set_new_rate/',{order_id:oid,client_new_rate:client_new_rate,agent_new_rate:agent_new_rate},function(data){ 
        if(data==1) 
        { 
         $('#rateupdated').html('<div id="display_message" class="msg msg-ok" style="margin:0 0 5px 0px;width:180px;"><p>Rate Updated Successfully!</p></div>'); 
         $("#rateupdated").fadeIn(0).fadeOut(5000); 
         $('#rec_ord_cnt_prc'+oid).html(client_new_rate); 
         $('#client_old_rate').html(client_new_rate); 
         $('#agent_old_rate').html(agent_new_rate); 
         $('#client_new_rate').val(''); 
         $('#agent_new_rate').val(''); 
        } 
       }); 
      } 
     } 
    }); 
} 

function add_note(oid) 
{ 
    alert("add_note"); 

    $("#TestForm").validate({ 
     event: "custom", 
     rules: { 
      note_description: { 
       required: true 
      } 
     }, 
     messages: { 
      note_description: { 
       required: "Required" 
      } 
     }, 
     submitHandler: function() { 
      alert("add_note _ sh"); 
      var note_description = $('#note_description').val(); 
      var note_type = $('input:radio[name=note_type]:checked').val(); 
      var answer = confirm("Add this "+note_type+" Note?") 
      if (answer) 
      { 
       $.post('http://localhost/fc/index.php/disp/add_note/',{order_id:oid,note_description:note_description,note_type:note_type},function(data){ 
        if(data==1) 
        { 
         $('#noteadded').html('<div id="display_message" class="msg msg-ok" style="margin:5px 5px 5px 0px;width:282px;"><p>Note Added Successfully!</p></div>'); 
         $('#note_description').val(''); 
         $("#noteadded").fadeIn(0).fadeOut(5000); 
        }        
       }); 
      } 
     } 
    }); 
} 

我使用jQuery验证插件。

+0

我不知道为什么要验证这种方式,我觉得如果你有单独的表单,因为他们做了不同的事情,这样会更容易 – 2011-04-26 15:24:22

回答

0

如何使用事件句柄中内置的jquery而不是submitHandler调用。

$('.MyForm .set_rate').click(function(e){ 
    set_new_rate(oid); 
    //here your submitHandler call function 
}); 

$('.MyForm .add_note').click(function(e){ 
    add_note(oid); 
    //here your submitHandler call function 
}); 

(我试图复制的功能,但textarea的给我的错误。在你刚功能的DELET在submitHandler两种情况。