【发布时间】:2016-09-13 13:56:52
【问题描述】:
我目前正在为我的打印机制造商雇主设计/编写一个新网站。客户可以通过网站注册下载软件和手册。
我想在注册表单中添加一点专业性,并想知道是否可以使用 jQuery。注意:我的 jQuery 知识非常有限,但我正在努力改进它!
我在页面中链接了 jquery、jquery easing 和 ui。
我希望处理的表单部分如下所示:
客户通常拥有 1 到 6 个系统,所有系统都有自己的序列号,可能还有许可证密钥。我最初只想显示选择系统选择框,然后一旦做出选择就显示序列号框。然后,一旦这也被填写,然后显示许可证密钥框和“添加另一个系统”按钮。
然后,如果客户有另一个系统,他们可以单击该按钮,它将添加第一个系统的克隆(即,在显示其他框和另一个添加系统按钮之前,添加一个要填写的新选择系统框)。唯一的例外是它可能需要一个删除按钮,以防客户点击添加太多次。 This new remove button could then be hidden when another system is selected.
类似这样:
我想将其限制为仅允许 6 组系统详细信息。每次在字段名称上修改一个数字。
当前html5的这一部分是这样的:
在底部忽略此现已更新的代码 (18/5/16)
<hr class="line">
<div class="eleven columns top-2 bottom-2">
<h3 class="title bottom-2">System Details</h3>
<div class="form-box">
<label>System <small>*</small>
</label>
<select name="system">
<option>Select system</option>
<option value="CPM-200">CPM-200</option>
<option value="CPM-100">CPM-100</option>
<option value="CPM-100H">CPM-100H</option>
<option value="CJ Pro">CJ Pro</option>
<option value="CJ-200C">CJ-200C</option>
<option value="CM-200">CM-200</option>
</select>
</div>
<div class="form-box">
<label>Serial Number <small>*</small>
</label>
<input type="text" name="serial1" class="text" required>
</div>
<!-- End Box -->
<div class="form-box">
<label>License Key (if known)</label>
<input type="text" name="license1" class="text" placeholder="e.g. ABCD1-EFGH2-IJKL3-MNOP4-QRST5" value="<?php echo htmlentities(strip_tags($company)); ?>">
</div>
<!-- End Box -->
<div class="form-box">
<a href="#" class="button medium secondary" style="margin-top: 35px; padding: 14px 30px;">Add Another System?</a>
</div>
<!-- End Box -->
</div>
<div class="clearfix"></div>
<input type="submit" value="Register" class="button medium color" />
如您所见,它相当复杂,而且问题很大!而且我不在我的联盟中,因此非常感谢一些友好的指点。
谢谢。
编辑 18/5
请原谅我的基本 jquery 代码,就像我说我不是专家一样,但希望你们都可以提出一些改进建议。目前,我使用 javascript 将 html 作为附加到包装器中添加,因为这允许我删除系统。
到目前为止,我已经设法弄清楚如何为每个系统添加 div,给它们 id,总共只允许 6 个。如果系统被删除,我也可以删除它们。
我不能做的是:
在第一个 id 上显示更多的序列框(因为我不知道如何修复名称后带有数字 x 的 id - 即 system5 以显示 serial5 框。想法?
如果客户删除了一个盒子,然后添加了一个新盒子,我如何使新盒子具有最低的可用 ID 号。即如果客户拥有所有六个系统,然后删除数字 4,如果他们想再次添加另一个系统,我如何让它找到那个 4 并添加一个新的系统 4?
目前的代码:
// Registration form element show / hide function
$(document).ready(function() {
var max_systems = 6; // maximum input boxes allowed
var wrapper = $(".system-wrap"); // Fields wrapper
var add_button = $(".system-button"); // Add button ID
var x = 1; // initial systems count
$(add_button).click(function(e){ // on add button click
e.preventDefault();
if(x < max_systems){ // max systems allowed
x++; // system increment
$(wrapper).append("<div class='system-wrap top-2 bottom-2'>" + "\n" + "<hr class='line bottom-2'>" + "\n" + "<div class='form-box'>" + "\n" + "<label>System <small>*</small></label>" + "\n" + "<select name='system" + x + "'>" + "\n" + "<option value=''>Select system</option>" + "\n" + "<option value='CPM-200'>CPM-200</option>" + "\n" + "<option value='CPM-100'>CPM-100</option>" + "\n" + "<option value='CPM-100H'>CPM-100H</option>" + "\n" + "<option value='CJ Pro'>CJ Pro</option>" + "\n" + "<option value='CJ-200C'>CJ-200C</option>" + "\n" + "<option value='CM-200'>CM-200</option>" + "\n" + "</select>" + "\n" + "</div>" + "\n\n" + "<div class='form-box' id='serial" + x + "'>" + "\n" + "<label>Serial Number <small>*</small></label>" + "\n" + "<input type='text' name='serial" + x + "' id='serialbox" + x + "' class='text' placeholder='e.g. 01234567L or CJP01-1234' value='' required>" + "\n" + "</div><!-- End Box -->" + "\n\n" + "<div class='form-box' id='license" + x + "'>" + "\n" + "<label>License Key (if known)</label>" + "\n" + "<input type='text' name='license" + x + "' class='text' placeholder='e.g. ABCD1-EFGH2-IJKL3-MNOP4-QRST5' value=''>" + "\n" + "</div><!-- End Box -->" + "\n" + "<div class='form-box remove-field'>" + "\n" + "<a href='#' class='button medium secondary' style='margin-top: 38px; padding: 14px 30px;'>Remove?</a>" + "\n" + "</div><!-- End Box -->" + "\n" + "</div>" + "\n\n" + "<div class='clearfix'></div>" + "\n"); // add remove button
}
});
$(wrapper).on("click",".remove-field", function(e){ // user click on remove text
e.preventDefault(); $(this).parent(".sys").remove(); x--;
})
});
$('#system').on('change',function(){
if ($(this).val() != "") {
$('#serial').show();
} else {
$('#serial').hide();
}
} );
$('#serialbox').on("keyup", function(){
if ($(this).val().length > 0) {
$('#license').show();
} else {
$('#license').hide();
}
} );
#serial, #license {
display: none;
}
<div class="eleven columns top-2 bottom-2 system-details">
<h3 class="title bottom-2">System Details</h3>
<p>You may register up to 6 different Lighthouse products below.</p>
<div class="systems top-2">
<div class="system-wrap">
<div class="form-box">
<label>System <small>*</small></label>
<select name="system" id="system">
<option value="">Select system</option>
<option value="CPM-200">CPM-200</option>
<option value="CPM-100">CPM-100</option>
<option value="CPM-100H">CPM-100H</option>
<option value="CJ Pro">CJ Pro</option>
<option value="CJ-200C">CJ-200C</option>
<option value="CM-200">CM-200</option>
</select>
</div>
<div class="form-box" id="serial">
<label>Serial Number <small>*</small></label>
<input type="text" name="serial" id="serialbox" class="text" placeholder="e.g. 01234567L or CJP01-1234" value="" required>
</div><!-- End Box -->
<div class="form-box" id="license">
<label>License Key (if known)</label>
<input type="text" name="license" class="text" placeholder="e.g. ABCD1-EFGH2-IJKL3-MNOP4-QRST5" value="">
</div><!-- End Box -->
<div class="clearfix"></div>
</div><!-- End System Wrap -->
</div><!-- End Systems -->
谢谢大家。
【问题讨论】:
-
请发布必要的
css和js你现在必须显示逐步控制.. -
我会将表单的那一部分包装在一个 div 中,然后您可以克隆它并在单击添加另一个按钮时追加 - 我将通过一个数组而不是更改克隆的名称输入,因为您必须在删除一个时将它们全部重命名(您可能需要可选字段的默认值)。 See this for multiple value with the same name
-
@Pete,当然我可以做数组,无论如何处理都是用 php 编码的,所以这不是问题。我想如果也有错误,我也可以将值反刍回表单中。