提交按钮不提交表单,但检查验证
提交按钮仅用于验证 Question.js 文件中的 javascript,但它不执行基本功能,即提交表单本身!非常感谢您的帮助。
`包含表单元素的 Ruby 页面代码
<script type="text/javascript">
loadScript("/javascripts/questions.js",function() {});
</script>
<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' %> <%= f.submit 'Cancel', :id=>'docNewCancelButton', :type=>'reset' %>
<% end %>
<br>
<hr>
<br>
<%= link_to 'Back', questions_path %>
question.js 文件中存在 Javascript 代码
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;
}
}
***question controller file***
# GET /questions/1
# GET /questions/1.xml
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
# GET /questions/new
# GET /questions/new.xml
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
The submit button works only for validating the javascript in the question.js file, but it doesnt do the basic function, which is submiting the form itself! Your help is very appreciated.
`Ruby page code containing the form element
<script type="text/javascript">
loadScript("/javascripts/questions.js",function() {});
</script>
<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' %> <%= f.submit 'Cancel', :id=>'docNewCancelButton', :type=>'reset' %>
<% end %>
<br>
<hr>
<br>
<%= link_to 'Back', questions_path %>
Javascript code present in question.js file
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;
}
}
***question controller file***
# GET /questions/1
# GET /questions/1.xml
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
# GET /questions/new
# GET /questions/new.xml
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来好像您正在使用 javascript 验证创建 ruby on Rails 表单。虽然您可以做到这一点,但我建议您从基本的 MVC 结构开始。我强烈建议您学习一些基础知识,以帮助您了解这一点。 http://guides.rails.info/ 是一个很好的起点。我希望它在我开始时就在那里,这会给我带来很大的痛苦:)
您将希望将验证转移到模型中。
在
app/models/question.rb
中,更多验证选项
那么您希望有一个控制器来响应您的创建事件。在
app/controller/question_controller.rb
中:然后你的
config/routes.rb
你的表单应该看起来与你所拥有的类似:
flash 很粗糙,我没有用了一段时间。我使用 message_block 插件。您还可以在此处阅读有关 Flash 如何工作的更多信息
。一些,但我建议你先做一些。它可以帮助您确定方向。祝你好运!
It appears as if you are doing a, ruby on rails form with javascript validations. While you can do that, I would recommend starting with a basic MVC structure. I highly recommend running through a some basics that will orient you to this. http://guides.rails.info/ is a great place to start. I wish that it had been there when I got started, it would have saved me a great deal of pain :)
The you will want to move your validations into the model.
in
app/models/question.rb
more baked in validation options here.
then you want to have a controller to respond to your create events. in
app/controller/question_controller.rb
:And then your
config/routes.rb
Your form should look similar to what you have:
the flash is crude, I haven't used it in a while. I use the message_block plugin. You can also read more about how the flash works here
There are plugins to shorten some of this, but I recommend cutting your teeth my doing some of this first. It helps you get oriented. Best of luck!