【发布时间】:2018-06-24 06:42:43
【问题描述】:
我正在处理一个表单页面,用户必须在其中插入至少两个字段:名称和价格。所以我用一个包含标签(用于字段名称)和一个输入项(用于字段值)的 div 来管理它。
字段名称和字段值必须在同一行。
字段名称必须在左侧,在有限宽度的 div 中,而字段值必须在右侧并在所有行上展开。 用户必须能够通过单击加号按钮来添加字段。
这是 HTML:
<div class="product-attributes">
<div class="product-attribute">
<label class="field-name">Name</label><input class="field-value" type="text" name="name">
</div>
<div class="product-attribute">
<label class="field-name">Price</label><input class="field-value" type="text" name="price">
</div>
</div>
<div class="product-attribute-add">
<label class="product-attribute-add-label">+</label>
</div>
这是 CSS:
.product-attributes {
width: 100%;
margin: 0;
padding: 0;
}
.product-attribute {
display:table;
width:100%
}
label,
.field-name {
display:table-cell;
margin: 0;
}
.product-attribute .field-name {
width: 80px;
}
.product-attribute-add-label {
margin: 0;
}
.field-value,
input[type="submit"] {
display:table-cell;
width: 100%;
margin: 0; ...
}
当用户点击加号标签时,我会附加一个新的 div:
addField = function() {
var attributeName = 'NEW';
$(this).parent().siblings('.product-attributes').append(
'<div class="product-attribute">'
+ '<input class="field-name" type="text" value="' + attributeName.charAt(0).toUpperCase() + attributeName.slice(1) + '"/>'
+ '<input class="field-value" type="text" name="' + attributeName + '"/>'
+ '</div>'
);
}
$('label.product-attribute-add-label').click(addField);
为什么他们不在同一条线上?我尝试了一切,但我不能这样做。如果我用标签切换潜水内的第一个输入,它们在同一行,但我不能用两个输入来做到这一点。
【问题讨论】:
标签: javascript jquery html css