使用jQuery我试图在id
函数重定向后通过window.location
向HTML元素添加文本。基本上是为了显示错误信息。
HTML.ERB
<p><%= text_field_tag 'birthday', nil, placeholder: 'mm/dd/yyyy' %></p>
<p id="birthday"></p>
的jQuery / JS
<% content_for :tail do %>
<script type="text/javascript">
$("#leading_health_yes_btn").on('click', function(){
var x = $("#gender").val();
var y = $("#hh_size").val();
var z = $("#birthday").val();
if ( validateBirthday(z) ){
$.ajax({ url: "/host_and_post/select?partner=LeadingHealthQuotes&gender="+x+"&hh_size="+y+"&birthday="+z });
} else {
window.location = "/sponsors";
$("#birthday").text("MUST BE mm/dd/yyyy FORMAT");
}
});
function validateBirthday(birthday){
var regexDate = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/
return regexDate.test(birthday);
};
</script>
<% end %>