我正在使用yii2
framkework並基於用戶提供的輸入我想創建input
字段。使用一些在線文檔我試圖做到這一點,但問題是我button
單擊事件不拿起jquery
使用javascript動態創建輸入字段
這裏是它的一個jsfiddle。不能完全複製我的問題,但我想要的是,如果用戶在輸入字段中輸入3時,應該在按鈕單擊時生成3字段。
這裏是我的view
代碼
<div class="event-form">
<?php $form = ActiveForm::begin(['id'=>'form-event']); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<div class="row">
<div class="col-lg-4 col-sm-4 col-xs-4">
<?= $form->field($model, 'start_date')->widget(
DatePicker::className(), [
'name' => 'start_date',
'options' => ['placeholder' => 'Select Event start date ...'],
'convertFormat' => true,
'pluginOptions' => [
'orientation' => 'bottom left',
'format' => 'MM/dd/yyyy',
'startDate' => date('dd-MM-yyyy'),
'autoclose' => true,
'todayHighlight' => true
]
])
?>
</div>
<div class="col-lg-4 col-sm-4 col-xs-4">
<?= $form->field($model, 'end_date')->widget(
DatePicker::className(), [
'name' => 'end_date',
'options' => ['placeholder' => 'Select Event end date...'],
'convertFormat' => true,
'pluginOptions' => [
'orientation' => 'bottom left',
'format' => 'MM/dd/yyyy',
'startDate' => date('dd-MM-yyyy'),
'autoclose' => true,
'todayHighlight' => true
]
])
?>
</div>
<div class="col-lg-4 col-sm-4 col-xs-4">
<button type="button" id="setdates" class="btn btn-default btn-set-dates">Set Dates</button>
</div>
</div>
<p></p>
<p></p>
<div class="form-group hide" id="optionTemplate">
<div class="col-xs-offset-3 col-xs-5">
<input class="form-control" type="text" name="option[]" />
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-danger' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
這裏是javascript
我使用
$('#setdates').click(function() {
startDate = new Date(document.getElementById('event-start_date').value);
endDate = new Date(document.getElementById('event-end_date').value);
var dates = [],
currentDate = startDate,
addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
});
$(document).ready(function() {
var MAX_OPTIONS = 5;
$('#form-event')
.on('click', '.setdates', function() {
var $template = $('#optionTemplate'),
$clone = $template
.clone()
.removeClass('hide')
.removeAttr('id')
.insertBefore($template),
$option = $clone.find('[name="option[]"]');
})
// Called after adding new field
.on('added.field.fv', function(e, data) {
if (data.field === 'option[]') {
if ($('#event-form').find(':visible[name="option[]"]').length >= MAX_OPTIONS) {
}
}
})
});
所以我不得不產生了dates
次輸入字段。
你能爲我們提供一個minmal,沒有按規定jsfiddle.net例子嗎? – Oleander
@Oleander我已經添加了jsfiddle和它不完全是我有什麼問題,但如果有人可以解決,比我會做其他 –
我只是'未捕獲的SyntaxError:意外的令牌{'。 – Oleander