0

以下代碼在我提交包含有效內容的表單時起作用。但是當我提交包含無效內容的表單時,它會驗證並顯示出現錯誤並重新加載頁面。但是,表單不會被提交。 你能幫我解決嗎? 謝謝。提交按鈕無法正常工作,但會檢查驗證

查看代碼

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

Javscript存在於questions.js代碼文件

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

是不是所需的行爲?您不希望提交表單,因爲它具有無效元素 – Jimmy 2010-06-27 20:07:18

+0

不。實際上,當無效表單再次顯示給用戶時,即使提供有效輸入,也不會提交 – felix 2010-06-27 20:29:47

回答

0

如果您不希望表單提交(例如,如果出現錯誤),請使用event.preventDefault()event.stopPropogation()return false。希望有所幫助。

0

由於致命錯誤,您的代碼中可能會出現異常。由於目前沒有try catch語句,因此此異常未被捕獲,請放棄event.preventDefault()並提交表單。

我會創建這個try catch語句並在其中添加Event.stop(event),以便在出現異常時不提交表單。