编写脚本以在提交之前将复选框值连接到字符串中
Script to join checkbox values into string before submitting
我有一个贝宝结账表单,我需要将多个选中的复选框值(name="cb1")连接到一个字符串中,以便将其作为隐藏输入(name="os1")值提交。我正在兜圈子想办法弄清楚。我对JS知之甚少!有人能帮忙吗?
<div id="buyform">
<form target="paypal" id="ppform" name="ppform" action="https://www.paypal.com/cgi-bin/webscr" method="post" >
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="myPPbusinessid">
<input type="hidden" name="lc" value="GB">
<input type="hidden" name="item_name" value="Form Title">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="add" value="1">
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHosted">
<div><input type="hidden" name="on0" value="Options"><h5>Options</h5></div>
<div>
<select name="os0" id="os0" required>
<option value="Chosen Product">Any quantity £10.00 GBP</option>
</select>
</div>
<input type="hidden" name="on1" value="Colours">
<div>
<input type="checkbox" name="cb1" value="Red" id="os10" class="thecbox"/><label for="os10"></label>
<input type="checkbox" name="cb1" value="Green" id="os11" class="thecbox"/><label for="os11"></label>
<input type="checkbox" name="cb1" value="Orange" id="os12" class="thecbox"/><label for="os12"></label>
<input type="checkbox" name="cb1" value="Purple" id="os13" class="thecbox"/><label for="os13"></label>
<input type="checkbox" name="cb1" value="Yellow" id="os14" class="thecbox"/><label for="os14"></label>
<input type="checkbox" name="cb1" value="Black" id="os15" class="thecbox"/><label for="os15"></label>
<input type="checkbox" name="cb1" value="Blue" id="os16" class="thecbox"/><label for="os16"></label>
<input type="hidden" name="os1" value="EACH, CHECKED, VALUE, HERE">
</div>
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="option_select0" value="Options">
<input type="hidden" name="option_amount0" value="10.00">
<input type="hidden" name="option_index" value="0">
<input type="hidden" name="currency_code" value="GBP">
<input type="submit" border="0" name="submit" id="submit" value="Purchase" class="buynow mb0"></form>
</div>
我似乎让它完全按照需要工作。但还是忍不住觉得JS需要整理一下!似乎很期待我怀疑应该是一段较小的代码。。。就像我说的:我的JS差到根本不存在!
<script type="text/javascript">
function checkTotal() {
document.ppform.os1.value = '';
if (document.getElementById('os10').checked){
var cb1str = document.ppform.cb1.value;
} else {
var cb1str = '';
}
if (document.getElementById('os11').checked){
var cb2str = document.ppform.cb2.value;
} else {
var cb2str = '';
}
if (document.getElementById('os12').checked){
var cb3str = document.ppform.cb3.value;
} else {
var cb3str = '';
}
if (document.getElementById('os13').checked){
var cb4str = document.ppform.cb4.value;
} else {
var cb4str = '';
}
if (document.getElementById('os14').checked){
var cb5str = document.ppform.cb5.value;
} else {
var cb5str = '';
}
if (document.getElementById('os15').checked){
var cb6str = document.ppform.cb6.value;
} else {
var cb6str = '';
}
if (document.getElementById('os16').checked){
var cb7str = document.ppform.cb7.value;
} else {
var cb7str = '';
}
var sum = cb1str.concat(cb2str).concat(cb3str).concat(cb4str).concat(cb5str).concat(cb6str).concat(cb7str);
document.ppform.os1.value = sum;
}
</script>
<div id="buyform">
<form target="paypal" id="ppform" name="ppform" action="https://www.paypal.com/cgi-bin/webscr" method="post" >
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="myPPbusinessid">
<input type="hidden" name="lc" value="GB">
<input type="hidden" name="item_name" value="Form Title">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="add" value="1">
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHosted">
<div><input type="hidden" name="on0" value="Options"><h5>Options</h5></div>
<div>
<select name="os0" id="os0" required>
<option value="Chosen Product">Any quantity £10.00 GBP</option>
</select>
</div>
<input type="hidden" name="on1" value="Colours">
<div>
<input type="checkbox" name="cb1" value="Red " id="os10" onchange="checkTotal()" /><label for="os10"></label>
<input type="checkbox" name="cb2" value="Green " id="os11" onchange="checkTotal()" /><label for="os11"></label>
<input type="checkbox" name="cb3" value="Orange " id="os12" onchange="checkTotal()" /><label for="os12"></label>
<input type="checkbox" name="cb4" value="Purple " id="os13" onchange="checkTotal()" /><label for="os13"></label>
<input type="checkbox" name="cb5" value="Yellow " id="os14" onchange="checkTotal()" /><label for="os14"></label>
<input type="checkbox" name="cb6" value="Black " id="os15" onchange="checkTotal()" /><label for="os15"></label>
<input type="checkbox" name="cb7" value="Blue " id="os16" onchange="checkTotal()" /><label for="os16"></label>
<input type="hidden" name="os1" value="">
</div>
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="option_select0" value="Options">
<input type="hidden" name="option_amount0" value="10.00">
<input type="hidden" name="option_index" value="0">
<input type="hidden" name="currency_code" value="GBP">
<input type="submit" border="0" name="submit" id="submit" value="Purchase" class="buynow mb0"></form>
</div>
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- 为复选框javascript指定两个值
- 如何在单击复选框后调用控制器方法
- 从复选框和Selects-KnockoutJS中获取值的总和
- 选中多个具有相同名称的复选框
- 使用Jquery选择或取消选择ListView中的所有复选框
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 使用$.ajax发布多个复选框
- AngularJS单选筛选不适用于Name、Description和Field4复选框值
- 使用jquery选中/取消选中单个复选框
- Angular JS Filter-通过3个复选框进行筛选
- 如果选中了多个复选框,如何添加事件
- Javascript复选框函数:;缺少:在属性id之后"
- 编写脚本以在提交之前将复选框值连接到字符串中
- Javascript:创建循环以连接选中复选框的结果
- Angularjs 将字符串与多复选框连接起来
- 为什么我的复选框不与我的jQuery连接
- 将我的复选框与动态选项卡连接
- 可以连接KML层以在一个复选框中显示它们(Google Maps)