提交按钮不起作用,但检查验证

时间:2010-06-27 18:30:38

标签: javascript ruby-on-rails javascript-events prototypejs scriptaculous

以下代码在我提交包含有效内容的表单时有效。但是当我提交包含无效内容的表单时,它会验证并显示存在错误并重新加载页面。但是表单没有提交。 你能帮我解决这个问题吗? 感谢。

查看代码

<h1 class="hdr1">Ask question</h1>

<%= link_to Profile.find(session[:user_id]).firstname,{},{:id=>"person"} %>
<% form_for(@question) do |f| %>
  <%= f.error_messages %>

<br>

<table id="newQuesTable" width="100%" cellpadding="5" border="1">
<tr>
    <td width="25%"><label>Your Question </label> - </td>
    <td><%= f.text_area :content, :rows=>5, :cols=>35, :maxlength=>500, :id=>"newQuesTxtA"%> </td>

<td width="30%"><i><label id="newQuesLabel" style="color:red;background-color:yellow;visibility:visible;"></label></i></td>

</tr>


<tr>
    <td width="25%"><%= f.label :tags %> -</td>
    <td><%= f.text_field :tags, :maxlength=>48, :id=>"tagsNewQuesTxt" %></td>
    <td width="30%"><i><label id="nquesTagsLabel" style="color:red;background-color:yellow;visibility:visible;"></label></i></td>
</tr>


<tr>
    <td>Question Scope -</td>
    <!--the open id for the hierarchy comes here-->
    <!-- the select box comes here -->
    <td> <%= f.text_field :ID_string %></td>
</tr>

</table>

<br>
    <%= f.submit 'Post Question' %> &nbsp; <%= f.submit 'Cancel', :id=>'docNewCancelButton', :type=>'reset' %>

<% end %>

<br>
<hr>
<br>    
<%= link_to 'Back', questions_path %>

问题.js文件中的Javscript代码

Event.observe(window, 'load', function(){
    $('new_question').observe('submit', submitQuestionCreate);
    $('quesNewCancelButton').onClick('resetquesform')
});

function resetquesform()
{
    event.preventDefault();
    reset($('new_question'));
    return;
}

function submitQuestionCreate(event)
{

    //event.preventDefault();

    var quesfield = $('newQuesTxtA');
    var tagsfield = $('tagsNewQuesTxt');
    var labelnques = $('newQuesLabel');
    var labelnquestags = $('nquesTagsLabel');

    if((quesfield.value == "") && (tagsfield.value == ""))
    {
        event.preventDefault();
        //alert('Question and Tags field cannot be empty');
        labelnques.innerHTML = 'Question field cannot be empty!';
        labelnquestags.innerHTML = 'Please enter (some) relevant tags...';
        probchk = true;
        return;
    }

    if((quesfield.value == "") || (tagsfield.value == ""))
    {
        event.preventDefault();
        if (quesfield.value == "")
        {
            labelnques.innerHTML = 'Question field cannot be empty!';
            labelnquestags.innerHTML = "";
            probchk = true;
            return;
        }

        if (tagsfield.value == "")
        {
            labelnquestags.innerHTML = 'Please enter (some) relevant tags...';
            labelnques.innerHTML = "";
            probchk = true;


            if (quesfield.value.length > 500)
            {
                labelnques.innerHTML = 'Question too long (should be 500 characters or less)';
                probchk = true;
            }

            return;
        }

    }


    if (quesfield.value.length > 500)
    {
        event.preventDefault();
        labelnques.innerHTML = 'Question too long (should be 500 characters or less)';
        probchk = true;
        return;
    }

}

控制器代码

 def show 
       @question = Question.find(params[:id]) 
       if !session[:user_id].nil? 
           #@owner = Document.is_owner(params[:id],session[:user_id]) 
           @fav_count = FavouriteQuestion.count(:all,:conditions=>["question_id = ? AND profile_id = 
                        ?",@question.id,session[:user_id]]) 
           if FavouriteQuestion.count(:all,:conditions=>["question_id = ? AND profile_id = 
                        ?",@question.id,session[:user_id]]) > 0 
                 @fav_status = 1 
           else 
                 @fav_status = 0 
           end
       else 
           @owner = Document.is_owner(params[:id],nil) 
           @fav_status = 0 
       end 
       respond_to do |format| 
          format.html # show.html.erb 
          format.xml { render :xml => @question } 
       end
   end

   def new 
       @question = Question.new 
       if !session[:user_id].nil? 
            @question.profile_id = session[:user_id] 
       end 
       respond_to do |format|  
           format.html # new.html.erb 
           format.xml { render :xml => @question } 
       end 
   end

2 个答案:

答案 0 :(得分:0)

如果您不希望提交表单(例如,如果出现错误),请使用event.preventDefault()event.stopPropogation(),或仅使用return false。希望有所帮助。

答案 1 :(得分:0)

由于致命错误,代码中可能会抛出异常。因为目前没有try catch语句,所以此异常未被捕获,请删除event.preventDefault()并提交表单。

我会创建这个try catch语句并在其中添加Event.stop(event),以便在出现异常时不提交表单。

相关问题