【发布时间】:2015-03-24 22:26:22
【问题描述】:
我有一个使用 Jquery 动态创建输入的表单。如果我硬编码一个与使用 Jquery 创建的元素完全相同的元素,我会得到很好的 POST 数据。但是当动态创建元素时,它们不会提交其 POST 数据。
用于创建元素的 Jquery(使用 Jquery-UI 拖放一个元素,当它被拖放时它会在该区域创建一个新元素)。
$(".drop-container").droppable({
accept: ".draggable",
addClasses: false,
drop: function (event, ui) {
var drag_name = ui.draggable.html();
ui.draggable.hide();
var new_body_count = '<tr><td class="btn btn-primary">' + drag_name + '</td><td class="badge"><input placeholder="Number of Kills" name="body_count" class="number-count" type="number"></td></tr>'
$('.table').prepend(new_body_count)
}
});
它创建的 HTML -
<tr>
<td class="btn btn-primary">
<span class="glyphicon glyphicon-resize-vertical" aria-hidden="true">
<input type="hidden" name="actor_id[]" value="162705079">
</span>
Julie Benz - J
</td>
<td class="badge">
<input placeholder="Number of Kills" name="body_count" class="number-count" type="number">
</td>
</tr>
它肯定在表单标签内。就像我说的,如果我对 html 进行硬编码,它可以发布,但是当输入是由 Jquery 创建时,没有发布。我很确定它与动态创建有关。
编辑 -
$('#update_form').on('submit', 'form', function(){
$('.abridged-hide input').attr('disabled', true);
$('.unabridged-hide input').attr('disabled', true);
});
编辑 - HTML 表单的代码
<legend class="text-center">Film title: <?=$film_title_text?></legend>
<div class="col-md-3">
<?=form_open('update/add_category');?>
<label for="category">Category Title</label>
<?=form_input($cat_title, '');?>
<button type="submit" class="btn btn-primary">Add Category</button>
<?=form_close();?>
</div>
<div id="event_form" class="col-md-6">
<?=form_open('/update/save_entry', $update_id);?>
<?php if(isset($thenav['nav_id'])){?>
<input type="hidden" name="film_id" value="<?=$q?>">
<div class="form-group">
<label for="add_category">Add Category</label>
<?=form_dropdown('cat_id', $cat, '', $cat_id );?>
</div>
<div class="form-group">
<label for="heading">Article Title</label>
<?=form_input($title, '');?>
</div>
<div class="well">
<div class="form-group">
<label for="heading">Add Custom Body Count Field</label>
<input type="text" class="form-control" name="body_count">
<button class="btn btn-primary" id="custom_body_cnt">Add Custom Body Count Field</button>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Abridged Cast</div>
<div class="panel-body abridged-hide">
<?php
foreach ($this_actors as $this_actor['name']) {
$characters = $this_actor['name']->name;
if(isset($characters[0])){
$characters = $characters[0];
}else{
$characters = '';
}
echo "<span class='btn btn-primary draggable'><input type='hidden' class='actor_field' name='actor_id[]' value='".$this_actor['name']->id."'><span class='glyphicon glyphicon-resize-vertical' aria-hidden='true'></span> ".$this_actor['name']->name." - ".$characters."</span>";
}?>
</div>
</div>
<button id="slide-abridged" class="btn wide-button"><span class='glyphicon glyphicon-resize-vertical' aria-hidden='true'></span></button>
<div class="panel panel-primary">
<div class="panel-heading">Unabridged Cast</div>
<button id="slide-unabridged" class="btn wide-button"><span class='glyphicon glyphicon-resize-vertical' aria-hidden='true'></span></button>
<div class="panel-body unabridged-hide">
<?php
foreach ($unabridged_actors as $this_actor['name']) {
$characters = $this_actor['name']->characters;
if(isset($characters[0])){
$characters = $characters[0];
}else{
$characters = '';
}
echo "<input type='text' name='actor_name' value='".$this_actor['name']->id."'>";
echo "<span class='btn btn-primary draggable'><span class='glyphicon glyphicon-resize-vertical' aria-hidden='true'></span> ".$this_actor['name']->name." - ".$characters."</span>";
}?>
</div>
</div>
<button id="slide-unabridged" class="btn wide-button"><span class='glyphicon glyphicon-resize-vertical' aria-hidden='true'></span></button>
<?=form_textarea($article, '');?>
</div>
<div class="col-md-3">
<span class="label label-default">Body Count Inventory</span>
<div class="well drop-container clearfix">
<table class="table">
<tr>
<td class="btn btn-primary">Total</td>
<td class="badge"><input placeholder="Number of Kills" name="film_total" class="film_total" disabled type="number"></td>
</tr>
</table>
</div>
<button type="submit" id="entry_submit" class="btn btn-primary">Update Page</button>
<?php }?>
<?=form_close()?>
</div>
【问题讨论】:
-
您想要
$('.table')中的点吗?你是说获取类名“table”的所有元素。仅此一项就对我有用$('table').prepend(new_body_count)在表格元素上。请注意,我删除了点。 -
不,它实际上是一个名为table的类。 ...可能是类名的错误选择。它确实在我希望它去的地方做前置。