gpt4 book ai didi

jquery - 使用 jQuery 和 Ajax 提交 Rails 表单

转载 作者:行者123 更新时间:2023-12-01 00:26:03 27 4
gpt4 key购买 nike

编辑:想通了,所以问了一个相关的问题。

这是我的 JavaScript

jQuery.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript")
}
})

jQuery.fn.submitWithAjax = function() {
this.submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
})
return this;
};

$('.error').hide();
$("#business_submit").click(function() {
// validate and process form here

$('.error').hide();
var name = $("input#business_name").val();
if (name == "" || name == "Required Field") {
$('#namelabel').show()
$("#business_name").focus();
return false;
}
var address = $("#business_address").val();
if (address == "" || address == "Required Field") {
$('#addresslabel').show();
$("#business_address").focus();
return false;
}
var city = $("#business_city").val();
if (city == "" || city == "Required Field") {
$('#citylabel').show();
$('#business_city').focus();
return false;
}
var state = $("#business_state").val();
if (state == "" || state == "Required Field") {
$('#statelabel').show();
$("#business_state").focus();
return false;
}
var zip = $("#business_zip").val();
if (zip == "" || zip == "Required Field") {
$('#ziplabel').show();
$("#business_zip").focus();
return false;
}
var phone = $("#business_phone").val();
if (phone == "" || phone == "Required Field") {
$('#phonelabel').show();
$("#business_phone").focus();
return false;
}

var category = $("#business_business_category_id").val();
if (category == " - Choose one - ") {
$('#categorylabel').show();
$("#business_business_category_id").focus();
return false;
}


$.ajax ({
type: "POST",
data: form.serialize()
});
return false;
})

.ajax 代码触发我的 create.js.erb 文件,其中包含

$("#new_business").submitWithAjax();
$("#new_business")[0].reset();
$("#new_business").hide();

这是表单下方的表格。

<div id="unapproved">
<table width="650" align="center" cellpadding="6" cellspacing="0">

<tr>
<td width="150"><a class="Contact<%=h @business.id %>" href="#"><%=h @business.name %></a></td>
<td width="150"><%=h @business.address %></td>
<td width="100"><%=h @business.business_category.name %></td>
<td width="200"><%=h @business.description %></td>
<td width="50"><%= link_to(image_tag('/images/accept.png', :alt => 'Approve'), :id =>@business.id, :action => 'approve') %>
<a class="Edit<%=h @business.id %>" href="#"><img src="/images/pencil.png" alt="Edit" /></a>
<%= link_to(image_tag('/images/bin.png', :alt => 'Remove'), @business, :confirm => 'Are you sure?', :method => :delete) %></td>
</tr>
</table>
</div>

现在我唯一的问题是我的表单下面的表格没有动态刷新。它将数据很好地添加到数据库中,并且所有验证都运行良好。但我必须刷新页面。我尝试添加类似的内容

$("#unapproved").append("<%= escape_javascript(render(:file => 'business')) %>");

但这只会破坏它。

最佳答案

我的猜测(因为您的“提交”按钮不在上面的 HTML 中,所以您的“business_submit”按钮只是一个输入而不是提交按钮。

这段代码:

jQuery.fn.submitWithAjax = function() {
this.submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
})
return this; };

不告诉表单提交。它说当“提交”发生时,去做某事。

From the jQuery documentation, submit(fn) is used to "Bind a function to the submit event of each matched element. The submit event fires when a form is submitted."

您实际上从未提交过表单。我愿意打赌(不能确定,因为我没有所有代码),您将 SubmitWithAjax 方法更改为:

jQuery.fn.submitWithAjax = function() {
$.post(this.action, $(this).serialize(), null, "script");
return false; };

它应该可以工作。它至少更接近您想要做的事情。

关于jquery - 使用 jQuery 和 Ajax 提交 Rails 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1133267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com