复选框脚本对索引中未选中的框进行计数
Checked box script is counting unchecked boxes in the index?
这个脚本从复选框中提取数据,以确定表单发送给PayPal的信息。PayPal只接受没有任何缺口的购物车;但是,此脚本对索引中使购物车无效的未选中框("item_name_1"、"item_name_3"、"item_name_7")进行计数。我如何使它,使脚本生成的数字("item_name_1","item_name_2","item_name_3")的间隙连续?
function updateCart(form) {
var cart = "";
var P = $('#P');
for (var i = 0; i < document.getElementById('sList').P.length; i++) {
if (document.getElementById('sList').P[i].checked)
cart += '<input type="hidden" name="item_name_' + (i+1) + '" value="' + document.getElementById('sList').P[i].value.substring(6) + '"><input type="hidden" name="amount_' + (i+1) + '" value="' + document.getElementById('sList').P[i].value.substring(0, [5]) + '">'
}
if (cart == "") {
alert("Please select products")
} else alert(cart);
$('#cart_items').html("" + cart);
return false;
}
除了编号之外,您的代码还有一些问题:
函数有一个从未使用过的form
参数。
document.getElementById()
返回单个DOM元素或null。P
变量是一个jQuery对象,鉴于你选择通过id,应持有一个元素或没有,给你试图使用一个索引P
你似乎认为它可以有多个匹配的元素,但只会是如果你有多个元素相同的id,这是无效的html(这将给不可靠的结果,所以如果是这样你应该改正:你可以具有相同名称的元素,但不是相同的id)。
无论哪种方式,尝试使用p作为.getElementById()
返回的DOM元素的属性都没有任何意义。所以你说的document.getElementById('').P.something
都是错的。
为隐藏的输入生成唯一编号,您只需要一个用于循环计数器的变量i
,然后第二个(新)变量用于输入计数,我们称其为n
。只在if checked
语句中增加n
。或者如果你用jQuery循环,你不需要i
,只需要n
。如果你显示你的HTML,我可以正确地更新,而不是猜测,但像这样:
function updateCart(form) {
// first remove any hidden inputs from a previous unsuccessful submit
// (this may be optional depending on whether you're submitting with ajax
// and/or potentially aborting the submit for other reasons, e.g., if you're
// submitting with ajax and the web server might return an error you need to
// remove the previous hiddens if the user tries again)
$('#cart_items input[type="hidden"]').remove();
// now process each checkbox and add numbered elements for each checked one
var n = 0,
$f = $("#cart_items");
// you may need to vary the selector here because
// I don't know the names of your elements
$('input[type="checkbox"]').each(function() {
if (this.checked) {
// following is a tidied up version of your input creation code,
// which assumes that the value attribute of the checkbox holds
// the item name and price information.
n++;
$f.append('<input type="hidden" name="item_name' + n +
'" value="' + this.value.substring(6) + '">');
$f.append('<input type="hidden" name="amount_' + n +
'" value="' + this.value.substring(0,5) + '">');
}
});
if (n === 0) {
// no hiddens were added
alert("Please select products");
return false;
} else {
// success, inputs added above
return true; // or submit or whatever
}
}
尝试使用自定义计数器:
function updateCart(form) {
var cart = "";
var P = $('#P');
var counter = 0;
for (var i = 0; i < document.getElementById('sList').P.length; i++) {
if (document.getElementById('sList').P[counter ].checked) {
cart += '<input type="hidden" name=" + (counter + 1) + '" value="' + document.getElementById('sList').P[counter].value.substring(6) + '"><input type="hidden" name="amount_' + (counter + 1) + '" value="' + document.getElementById('sList').P[counter].value.substring(0, [5]) + '">';
counter++;
}
}
if (cart == "") {
alert("Please select products")
} else alert(cart);
$('#cart_items').html("" + cart);
return false;
}
相关文章:
- 名称输入的索引
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 借助asp.net验证或java脚本对多个文本进行验证
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 在jQuery中获取表的行索引
- Disqus的计数.js脚本在索引中无法正常运行.html与react.js网站
- 仅在 url 索引上运行脚本
- 如何删除索引中加载的脚本文件中的重复项.html和业力
- 如何使用 Java 脚本中的最后一个索引删除子字符串
- 从我的索引.html中的<脚本>标签中发出 GET 请求
- 如何对以下 js 索引脚本进行故障排除
- 如何允许另一个索引字符串运行脚本
- AJAX调用中缺少POST参数会导致PHP脚本中出现未定义的索引警告
- 复选框脚本对索引中未选中的框进行计数
- 如何在java脚本高图中实现z索引
- 单选按钮选择索引更改,当添加icheck.js脚本引用时不触发
- 使用Java脚本获取指定行的行索引
- Java脚本使用文本索引突出显示文本
- 分配,在主索引页上为脚本使用唯一的Wordpress帖子id
- 在Java脚本中索引超出了数组的边界,c# Web应用程序创建谷歌图表