yii2 ajax提交自定义表单什么都不做(yii2 ajax submit custom form do nothing)
**你好
我想用ajax提交我的表单值我在模态中的视图文件中有一个模态我构建了一个自定义表单来将我的数据提交给一个动作**
use yii\bootstrap\Modal;
use yii\helpers\Html;
use yii\helpers\Url;
Modal::begin([
'header' => 'روز تخمینی برای اتمام پروژه انتخاب کنید',
'id' => 'modal-accept',
'size' => 'modal-sm',
'clientOptions' => [
'backdrop' => 'static',
'keyboard' => false,
],
]);
?>
echo jDate\DatePicker::widget([
'name' => 'datepicker',
'id' => 'estimate',
]);
?>
= Html::submitButton('submit',['class' => 'btn btn-sm btn-success']) ?>
Modal::end();
?>
提交我的价值并获得结果我使用此功能,,,但是当我点击提交没有ajax调用将执行什么都不会发生为什么我不能看到我的浏览器中的任何网络活动检查...
$('#modal_form').on('beforeSubmit', function(e) {
var form = $(this);
var formData = form.serialize();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
success: function (data) {
console.log("successfull");
},
error: function () {
alert("Something went wrong");
}
});
}).on('submit', function(e){
e.preventDefault();
});
有什么建议吗?
**hello
i want to submit my form value with ajax i have a modal in my view file in modal i built a custom form to submit my data to a action**
use yii\bootstrap\Modal;
use yii\helpers\Html;
use yii\helpers\Url;
Modal::begin([
'header' => 'روز تخمینی برای اتمام پروژه انتخاب کنید',
'id' => 'modal-accept',
'size' => 'modal-sm',
'clientOptions' => [
'backdrop' => 'static',
'keyboard' => false,
],
]);
?>
echo jDate\DatePicker::widget([
'name' => 'datepicker',
'id' => 'estimate',
]);
?>
= Html::submitButton('submit',['class' => 'btn btn-sm btn-success']) ?>
Modal::end();
?>
for submit my value and get the result i used this function ,,, but when i click submit no ajax call will perform nothing will happening why i cant see any network activity in my browser inspect ...
$('#modal_form').on('beforeSubmit', function(e) {
var form = $(this);
var formData = form.serialize();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
success: function (data) {
console.log("successfull");
},
error: function () {
alert("Something went wrong");
}
});
}).on('submit', function(e){
e.preventDefault();
});
any suggestion?
原文:https://stackoverflow.com/questions/45920739
更新时间:2019-10-07 09:02
最满意答案
在submsision之前你不需要使用,这就是问题所在
$('#modal_form').on('submit', function(e){
e.preventDefault();
var form = $(this);
var formData = form.serialize();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
success: function (data) {
console.log("successfull");
},
error: function () {
alert("Something went wrong");
}
});
return false;
});
You don't need to use before submsision, this is the problem
$('#modal_form').on('submit', function(e){
e.preventDefault();
var form = $(this);
var formData = form.serialize();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: formData,
success: function (data) {
console.log("successfull");
},
error: function () {
alert("Something went wrong");
}
});
return false;
});
相关问答
Yii ActiveForm在其生命周期的许多阶段都支持JavaScript事件。 您可以使用beforeSubmit事件来实现您正在查找的内容。 在JavaScript中: $(document).ready(
$('#myform').on('beforeSubmit', function(event, jqXHR, settings) {
var form = $(this);
if(form.find('.has-error').length) {
...
请看看demos @ http://demos.krajee.com/widget-details/select2 您可以使用Select2而不使用ActiveForm,甚至不使用Model。 从演示示例中,下面是相关的代码片段: use kartik\widgets\Select2
// With a model and without ActiveForm
echo Select2::widget([
'model' => $model,
'attribute' => 'st
...
您可以在成功回调中使用ajax表单提交和启用按钮。 假设你的from的id是myForm ,你的Add Slider和Add Attchment按钮分别有ids add-slider和add-attachment 。 $(document).ready(
$('#myform').on('beforeSubmit', function(event, jqXHR, settings) {
var form = $(this);
if(form.find('.h
...
您不需要返回任何内容,只需将该错误添加到属性就足够了。 从版本2.0.11您可以使用yii\validators\InlineValidator::addError()来添加错误,而不是使用$this 。 这样,错误信息可以使用yii\i18n\I18N::format()立即yii\i18n\I18N::format() 。 使用错误消息中的{attribute}和{value}来引用属性标签(无需手动获取)和相应的属性值: 我怀疑你的情况是你缺少$formModel->validate() ,
...
虽然您可以在情景中使用Conditional Validators when但我会建议使用更复杂的方式来定义自定义验证程序,因为根据文档 要创建一个支持客户端验证的验证器,您应该实现yii\validators\Validator::clientValidateAttribute()方法,该方法返回一段JavaScript代码,在客户端执行验证。 在JavaScript代码中,您可以使用以下预定义变量: attribute:正在验证的属性的名称。 value:正在验证的值。 messages:用于
...
将它包含在ActiveForm配置中是不够的。 请求可以被发送,但表单将保持不变。 您还应该准备您的控制器(在CRUD生成的控制器中,它们是create和update ,在您的情况下,它可以只是一个 - 负责更新密码),以便在AJAX请求的情况下返回正确的JSON数据。 了解它是如何在官方文档中完成的。 It's not enough to just include it in ActiveForm config. The requests can be sended but the form w
...
在submsision之前你不需要使用,这就是问题所在 $('#modal_form').on('submit', function(e){
e.preventDefault();
var form = $(this);
var formData = form.serialize();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
dat
...
问题 ajax中的问题是$_FILES细节不会在异步请求中发送。 当我们提交没有Ajax请求的填充表单并在PHP的后端进行调试时 echo '
';
print_r($_FILES);
print_r($_POST);
echo '
';die;
那么我们会成功获取$_FILES和$_POST数据。 但是当我们在ajax请求中调试同样的东西时,我们只获得$_POST值,并且我们将$_FILES作为NULL 。 这导致我们得出结论, $_FILES数据不是由我们的上述代码
...
你为什么不用Pjax? 使用yii \ widgets \ Pjax并在表单周围添加Pjax :: begin()和Pjax :: end()。 这是一个不错的指南: http : //www.yiiframework.com/wiki/772/pjax-on-activeform-and-gridview-yii2/ Why don't you use Pjax? Use yii\widgets\Pjax and add Pjax::begin() and Pjax::end() around
...