我正在尝试正确地设置submit事件(onsubmit设置为内联),但它将被触发两次,这不是期望的行为。
我的
活性形式
代码:
<?php
use app\models\Countries;
use app\models\States;
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
$form = ActiveForm::begin(
[
'action' => 'user-default-shipping/create',
'options' => [
'onsubmit' => 'Address.createDefault(event, this)'
]
]
);
if ($model->id)
echo Html::activeHiddenInput($model, 'id');
echo $form->field($model, 'city')->textInput();
echo $form->field($model, 'address')->textInput();
echo $form->field($model, 'zip')->input('text');
echo $form->field($model, 'country_id')
->dropDownList(ArrayHelper::map(Countries::find()->all(), 'country_id', 'country_name'),
['role' => 'countries']
)->hint('Choose your country.');
echo $form->field($model, 'state_id')
->dropDownList(ArrayHelper::map(States::find()->all(), 'state_id', 'state_name'),
[
'role' => 'states'
]
)->hint('Choose your state.');
?>
<div class="form-group">
<button type="submit" class="btn btn-green">Submit</button>
</div>
<?php ActiveForm::end(); ?>
JS代码:
const Address = {
createDefault(event, form) {
event.preventDefault();
console.log(form);
}
}
有没有办法防止这种行为?