2013-07-16 42 views
0

我正在做一个rails项目。这里说到我的问题:我有一个button_to标签在我.haml文件:js in rails:关于onclick

= button_to "Add", add_problem_path(paper, :problem_id => problem.id), :class => 'essay_problem', :id => problem.id, :remote => true, :onclick => 'new function() { if($(".essay#3").val() == "xxxx"){ alert("Please add the essay first!"); return false; } }'

我想要做的就是让被onclick()验证之前发布到add_problem_path,如果验证通过则发布到add_problem_path,或者它只是提醒并且什么也不做。我该怎么办?

回答

0

你只是定义一个新的功能在onclick事件

创建下面的功能:在HAML文件中的JavaScript。在你的onclick函数中调用它

= button_to "Add", add_problem_path(paper, :problem_id => problem.id), :class => 'essay_problem', :id => problem.id, :remote => true, :onclick => 'return validate();' 

:javascript 
    function validate() 
    { 
    if($(".essay#3").val() == "xxxx"){ 
    alert("Please add the essay first!"); 
    return false; 
    } 
    else 
    { 
    return true; 
    } 
    } 
+0

我明白了。当'onclick'返回false时,button_to将不会发布到add_problem_path。在以前,我只是在onclick事件中返回false,这意味着返回onclick事件。非常感谢你的帮助! – kgtong