Javascript 为动态HTML元素添加值
我使用Laravel5构建简单的CMS。 我根据属性表中的值创建HTML元素。 所以,如果我有3个属性,如:高度、重量、尺寸,那么我循环该值以创建HTML元素:Javascript 为动态HTML元素添加值,javascript,jquery,html,laravel-5.1,Javascript,Jquery,Html,Laravel 5.1,我使用Laravel5构建简单的CMS。 我根据属性表中的值创建HTML元素。 所以,如果我有3个属性,如:高度、重量、尺寸,那么我循环该值以创建HTML元素: <input type='text' name='height' /> <input type='text' name='weight' /> <input type='text' name='dimension' /> 我希望将该数据传递到指定的HTML元素,在上面的示例中,我希望将该数据传递到:
<input type='text' name='height' />
<input type='text' name='weight' />
<input type='text' name='dimension' />
我希望将该数据传递到指定的HTML元素,在上面的示例中,我希望将该数据传递到:
因此,按照我的逻辑,我必须:
根据属性表值循环创建HTML。
来自控制器的循环数据
比较一下,若attribute\u id==product->attribute\u id,那个么我创建HTML元素并设置它的值。
以下是我的实现:
<div class="box-body">
@if ($attributes != null)
@foreach ($attributes as $attr)
<div class="form-group">
{!! Form::label('', $attr->attribute_name) !!}
@foreach ($product->attributes as $pAttr)
@if ($attr->id == $pAttr->id)
@if ($attr->type == "text")
<input type="text" name="attributes[{{ $attr->id }}][value]" value="{{ $pAttr->pivot->value }}" class="form-control" />
@else
<textarea id="content2" name="attributes[{{ $attr->id }}][value]">{{ $pAttr->pivot->value }}</textarea>
@endif
@else
@if ($attr->type == "text")
<input type="text" name="attributes[{{ $attr->id }}][value]" value="" class="form-control" />
@else
<textarea id="content2" name="attributes[{{ $attr->id }}][value]"></textarea>
@endif
@endif
@endforeach
</div>
@endforeach
@endif
</div>
但是,我得到以下结果:
如何解决这个问题?
谢谢试试这个:
<div class="box-body">
@if ($attributes != null)
{{-- START: loop attributes --}}
@foreach ($attributes as $attr)
<div class="form-group">
{{-- START: loop product's attributes --}}
@foreach ($product->attributes as $pAttr)
@if ($attr->type == "text")
<input type="text" name="attributes[{{ $attr->id }}][value]" value="{{ ($attr->id == $pAttr->id)? $pAttr->pivot->value : '' }}" class="form-control" />
@else
<textarea id="content2" name="attributes[{{ $attr->id }}][value]">{{ ($attr->id == $pAttr->id)? $pAttr->pivot->value : '' }}</textarea>
@endif
@endforeach
{{-- END: loop product's attributes --}}
</div>
@endforeach
{{-- END: loop attributes --}}
@endif
</div>
我已经删除了标签,因为里面没有文本
答复✔
我已经有了解决我问题的办法。我构建动态HTML
我的控制器中的元素。所以控制器将传递HTML元素
具体值
这是我的密码:
$attrElements = array();
foreach ($attributes as $attr) {
$element = array();
if ($attr->type == 'text') {
$element = array(
'id' => $attr->id,
'name' => $attr->attribute_name,
'type' => 'text',
'element' => "<input type='text' name='attributes[". $attr->id ."][value]' value='' class='form-control' />"
);
} else {
$element = array(
'id' => $attr->id,
'name' => $attr->attribute_name,
'type' => 'editor',
'element' => "<textarea name='attributes[". $attr->id ."][value]' class='form-control'></textarea>"
);
}
array_push($attrElements, $element);
}
foreach ($attrElements as &$attrEl) {
foreach ($product->attributes as $pAttr) {
if ($pAttr->id == $attrEl['id']) {
// echo $pAttr->id . " == " . $attrEl['id'] . " ? " . ($pAttr->id == $attrEl['id']);
// echo "<br/>";
if ($attrEl['type'] == 'text') {
$attrEl['element'] = "<input type='text' class='form-control' name='attributes[". $attrEl['id'] ."][value]' value='" . $pAttr->pivot->value . "' />";
} else {
$attrEl['element'] = "<textarea name='attributes[". $attrEl['id'] ."][value]' class='form-control'>" . $pAttr->pivot->value . "</textarea>";
}
}
}
}
它是有效的!:D
谢谢
$attrElements = array();
foreach ($attributes as $attr) {
$element = array();
if ($attr->type == 'text') {
$element = array(
'id' => $attr->id,
'name' => $attr->attribute_name,
'type' => 'text',
'element' => "<input type='text' name='attributes[". $attr->id ."][value]' value='' class='form-control' />"
);
} else {
$element = array(
'id' => $attr->id,
'name' => $attr->attribute_name,
'type' => 'editor',
'element' => "<textarea name='attributes[". $attr->id ."][value]' class='form-control'></textarea>"
);
}
array_push($attrElements, $element);
}
foreach ($attrElements as &$attrEl) {
foreach ($product->attributes as $pAttr) {
if ($pAttr->id == $attrEl['id']) {
// echo $pAttr->id . " == " . $attrEl['id'] . " ? " . ($pAttr->id == $attrEl['id']);
// echo "<br/>";
if ($attrEl['type'] == 'text') {
$attrEl['element'] = "<input type='text' class='form-control' name='attributes[". $attrEl['id'] ."][value]' value='" . $pAttr->pivot->value . "' />";
} else {
$attrEl['element'] = "<textarea name='attributes[". $attrEl['id'] ."][value]' class='form-control'>" . $pAttr->pivot->value . "</textarea>";
}
}
}
}