Javascript 复选框将所有已完成的输入字段复制到下面的字段
我有以下代码Javascript 复选框将所有已完成的输入字段复制到下面的字段,javascript,jquery,html,Javascript,Jquery,Html,我有以下代码OnClick复选框,我想将每个完整的input复制到下面的复选框中 我可以用一个长Jquery脚本来实现这一点。但一定有办法用几行就能做到这一点?有人能解释一下这个概念吗 以下是: 我的代码是: <form action="?action=processOrder" method="post" id="hidden"> <label> <input type="radio" name="paymentMethod" value=
OnClick
复选框,我想将每个完整的input
复制到下面的复选框中
我可以用一个长Jquery
脚本来实现这一点。但一定有办法用几行就能做到这一点?有人能解释一下这个概念吗
以下是:
我的代码是:
<form action="?action=processOrder" method="post" id="hidden">
<label>
<input type="radio" name="paymentMethod" value="Cash on Delivery" checked>
<p style="float: left;margin: -2px 0 0 9px;">Cash on Delivery</p>
</label>
<br>
<label>
<input type="radio" name="paymentMethod" value="Pay Online" id="payOnline">
<p style="float: left;margin: -2px 0 0 9px;">Pay Online</p>
</label>
<fieldset id="contact-inputs">
<input id="deliveryAddressLineOne" type="text" name="deliveryAddressLineOne" placeholder="Address line one" required>
</fieldset>
<fieldset id="contact-inputs">
<input id="deliveryAddressLineTwo" type="text" name="deliveryAddressLineTwo" placeholder="Address line two" required>
</fieldset>
<fieldset id="contact-inputs">
<input id="deliveryAddressTown" type="text" name="deliveryAddressTown" placeholder="Town" required>
</fieldset>
<fieldset id="contact-inputs">
<input id="deliveryAddressCounty" type="text" name="deliveryAddressCounty" placeholder="County" required>
</fieldset>
<fieldset id="contact-inputs">
<input id="deliveryAddressPostCode" type="text" name="deliveryAddressPostCode" placeholder="Post Code" required>
</fieldset>
<!-- if pay online checked, show the other fields -->
<label class="billingAddress" style="display:none;">My billing address is the same as delivery.
<input type="checkBox" placeholder="Same as Delivery" id="duplicateDelivery">
</label>
<fieldset id="contact-inputs">
<input class="billingAddress" type="text" name="billingAddressLineOne" placeholder="Address line one" required>
</fieldset>
<fieldset id="contact-inputs">
<input class="billingAddress" type="text" name="billingAddressLineTwo" placeholder="Address line two" required>
</fieldset>
<fieldset id="contact-inputs">
<input class="billingAddress" type="text" name="billingAddressTown" placeholder="Town" required>
</fieldset>
<fieldset id="contact-inputs">
<input class="billingAddress" type="text" name="billingAddressCounty" placeholder="County" required>
</fieldset>
<fieldset id="contact-inputs">
<input class="billingAddress" type="text" name="billingAddressPostCode" placeholder="Post Code" required>
</fieldset>
<fieldset>
<input id="processOrder" class="green-button" type="submit" name="saveChanges" value="Process Order" />
</fieldset>
</form>
我建议使用带有布尔开关的
toggle()
方法:
$('input[type="radio"]').click(function () {
var check = this.id === 'payOnline';
// select all the elements you want to show/hide:
$('.billingAddress, #County, #postCode')
// using a Boolean switch will show the collection
// if it is, or evaluates to, true (or truthy) and
// hide them if it is, or evaluates to, false (or falsey):
.toggle(check);
if (!check) {
// this is in an 'if' because you've only shown one state,
// though I suspect that's an error by omission, and
// toggle(!check) is more likely what you want:
$('#processOrder').show();
}
$('input').prop('required', check);
});
尽管值得注意的是,最终选择器将禁用所有
元素,包括将单击事件处理程序绑定到的单选按钮
参考资料:
我建议使用带有布尔开关的toggle()
方法:
$('input[type="radio"]').click(function () {
var check = this.id === 'payOnline';
// select all the elements you want to show/hide:
$('.billingAddress, #County, #postCode')
// using a Boolean switch will show the collection
// if it is, or evaluates to, true (or truthy) and
// hide them if it is, or evaluates to, false (or falsey):
.toggle(check);
if (!check) {
// this is in an 'if' because you've only shown one state,
// though I suspect that's an error by omission, and
// toggle(!check) is more likely what you want:
$('#processOrder').show();
}
$('input').prop('required', check);
});
尽管值得注意的是,最终选择器将禁用所有
元素,包括将单击事件处理程序绑定到的单选按钮
参考资料:
我建议使用带有布尔开关的toggle()
方法:
$('input[type="radio"]').click(function () {
var check = this.id === 'payOnline';
// select all the elements you want to show/hide:
$('.billingAddress, #County, #postCode')
// using a Boolean switch will show the collection
// if it is, or evaluates to, true (or truthy) and
// hide them if it is, or evaluates to, false (or falsey):
.toggle(check);
if (!check) {
// this is in an 'if' because you've only shown one state,
// though I suspect that's an error by omission, and
// toggle(!check) is more likely what you want:
$('#processOrder').show();
}
$('input').prop('required', check);
});
尽管值得注意的是,最终选择器将禁用所有
元素,包括将单击事件处理程序绑定到的单选按钮
参考资料:
我建议使用带有布尔开关的toggle()
方法:
$('input[type="radio"]').click(function () {
var check = this.id === 'payOnline';
// select all the elements you want to show/hide:
$('.billingAddress, #County, #postCode')
// using a Boolean switch will show the collection
// if it is, or evaluates to, true (or truthy) and
// hide them if it is, or evaluates to, false (or falsey):
.toggle(check);
if (!check) {
// this is in an 'if' because you've only shown one state,
// though I suspect that's an error by omission, and
// toggle(!check) is more likely what you want:
$('#processOrder').show();
}
$('input').prop('required', check);
});
尽管值得注意的是,最终选择器将禁用所有
元素,包括将单击事件处理程序绑定到的单选按钮
参考资料:
不错!没有想到用逗号分隔选择器。但是要小心-您错过了$(“#processOrder”)
(如果不是付款行,则会显示,但不会隐藏)。谢谢David,正如DeeMac提到的,我没有想到用逗号分隔选择器。伟大的假设用户填写了deliverAddress输入,如果勾选了复选框#duplicateDelivery,我如何将这些值复制到BillingAddress输入?应该更新我原来的问题,使之更清楚。很好!没有想到用逗号分隔选择器。但是要小心-您错过了$(“#processOrder”)
(如果不是付款行,则会显示,但不会隐藏)。谢谢David,正如DeeMac提到的,我没有想到用逗号分隔选择器。伟大的假设用户填写了deliverAddress输入,如果勾选了复选框#duplicateDelivery,我如何将这些值复制到BillingAddress输入?应该更新我原来的问题,使之更清楚。很好!没有想到用逗号分隔选择器。但是要小心-您错过了$(“#processOrder”)
(如果不是付款行,则会显示,但不会隐藏)。谢谢David,正如DeeMac提到的,我没有想到用逗号分隔选择器。伟大的假设用户填写了deliverAddress输入,如果勾选了复选框#duplicateDelivery,我如何将这些值复制到BillingAddress输入?应该更新我原来的问题,使之更清楚。很好!没有想到用逗号分隔选择器。但是要小心-您错过了$(“#processOrder”)
(如果不是付款行,则会显示,但不会隐藏)。谢谢David,正如DeeMac提到的,我没有想到用逗号分隔选择器。伟大的假设用户填写了deliverAddress输入,如果勾选了复选框#duplicateDelivery,我如何将这些值复制到BillingAddress输入?应该更新我原来的问题,让它更清楚。出于好奇,你是不是故意只显示processOrder
而从不隐藏它?似乎与您编写的其他代码不一致。此小提琴不起作用。它不包括jquerycode@DeeMacprocessOrder在重定向到允许用户确认/编辑提供的详细信息及其注册的用户详细信息的页面时始终显示。在我的php中有一个开关,它检查所检查的收音机,然后对这些收音机执行所需的操作。如果你认为有更好的方法,我会很乐意听你的?KaustavBanerjee我已经更新了fiddle,不知道为什么它没有包含jQuery。出于好奇,你是不是故意只显示processOrder
而从不隐藏它?似乎与您编写的其他代码不一致。此小提琴不起作用。它不包括jquerycode@DeeMacprocessOrder在重定向到允许用户确认/编辑提供的详细信息及其注册的用户详细信息的页面时始终显示。在我的php中有一个开关,它检查所检查的收音机,然后对这些收音机执行所需的操作。如果你认为有更好的方法,我会很乐意听你的?KaustavBanerjee我已经更新了fiddle,不知道为什么它没有包含jQuery。出于好奇,你是不是故意只显示processOrder
而从不隐藏它?似乎与您编写的其他代码不一致。此小提琴不起作用。它不包括jquerycode@DeeMacprocessOrder在重定向到允许用户确认/编辑提供的详细信息及其注册的用户详细信息的页面时始终显示。在我的php中有一个开关,它检查所检查的收音机,然后对这些收音机执行所需的操作。如果你认为有更好的方法,我会很乐意听你的?KaustavBanerjee我已经更新了fiddle,不知道为什么它没有包含jQuery。出于好奇,你是不是故意只显示processOrder
而从不隐藏它?似乎与您编写的其他代码不一致。此小提琴不起作用。它不包括jquerycode@DeeMacprocessOrder在重定向到允许用户确认/编辑提供的详细信息及其用户详细信息的页面时始终显示