Use javascript create checkbox dynamically

本文介绍如何使用JavaScript动态生成和增加Checkbox元素,适用于不确定初始显示数量的场景,并提供具体实现代码。

 在实际使用中,可能在界面第一次展示的时候,不能确定到底要多少个checkbox,这和业务的需求要关系。

 有一种方法就是在页面元素加载完成后,依靠js动态产生一定数量的checkbox,并且如果需要增加时,也可以在js中动态增加。

在js中动态生成html元素的方法不仅对check box,对select标签元素也是一样的,大同小异。


代码如下:


   jsp中,写一个标签,这是id是要用到的。

<tr>
    <td align="center" colspan="3" id="check_box_group"></td>
</tr>

js中,代码如下,效果就是在jsp上述的td标签下添加若干个<input type="checkbox" name="checkName"  value="具体的数字">标签

// 构建checkbox框
function showcheckbox() {
	var box = $("check_box_group");
	var _html = box.innerHTML;
	var type = "checkbox";
	
	for ( var i = 0; i <= 9; i++) {
		_html += "  <input type='" + type + "' name='checkName' value='" + i + "'>" + i ;
	}
	box.innerHTML = _html + "</br>";
}</span>


在此基础上,可以设置按键,继续增加checkbox的数量,方法和上面的方法类似,要注意的是value的值要和上面的接上。

//增加checkbox
function add_check_box() {
	var type = "checkbox";
	
	var box = $("check_box_group");
	var _html = box.innerHTML;
	var cldNum = document.getElementsByName("checkName");
	for ( var i = checkName.length; i <checkName.length + 10; i++) {  //增加10个
		_html += "  <input type='" + type + "' name='checkName' value='" + i + "'>:" + i;
	}
	box.innerHTML = _html + "</br>";
}

选择个别checkbox后,点击确定如果是子window对象,那么需要parent.document.getxxx获取父window中的对象


var Nums="";
var n=0;

Ext.each(Ext.query("[name='checkName']"),function(item){
		if(item.checked){
			n++;
			Nums = Nums + item.value + ",";
		}
	});
	Nums= Nums.substr(0, cldNums.length - 1);

parent.document.getElementsByName("xxx")[xx].value = cNums; //赋值给父window下的对象parent.win.close();

</pre><pre>
附加:jsNode.cloneNode();//该方法可以复制一个完成的node,如整个tr标签节点。<tr><td></td>...</tr>


AI Overview Implementing selective checkout with checkboxes on a cart page typically involves allowing users to select specific items in their cart for purchase, rather than requiring them to purchase all items. This functionality is not a standard feature in most e-commerce platforms and usually requires custom development or the use of specialized plugins/apps. General Approach: Modify the Cart Template: Add a checkbox next to each item in the cart. Ensure the checkbox is linked to the specific product or line item. Handle User Selections: Use JavaScript to detect changes in checkbox states (checked/unchecked). When a checkbox is selected or deselected, update the cart's subtotal and potentially the items that will be included in the checkout process. Adjust the Checkout Process: When the user proceeds to checkout, ensure that only the items corresponding to the selected checkboxes are passed to the order. This may involve modifying the platform's core checkout logic or using hooks/filters provided by the platform. Platform-Specific Considerations: WooCommerce (WordPress): Requires custom code in functions.php or a custom plugin to add checkboxes, manage selections, and modify the checkout process. You might use AJAX to update the cart dynamically as selections are made. Shopify: Involves modifying theme files (e.g., cart-template.liquid, theme.js) to add checkboxes and JavaScript to handle the logic. This often requires familiarity with Liquid (Shopify's templating language) and JavaScript. Other Platforms: The specific implementation will vary based on the platform's architecture and extensibility options. It may involve similar approaches of template modification and custom code. Important Notes: Complexity: Implementing selective checkout can be complex and may require advanced coding skills. User Experience: Carefully consider the user experience implications of selective checkout to ensure it is intuitive and does not cause confusion. Testing: Thoroughly test the functionality to ensure that selected items are correctly processed and that no loopholes exist (e.g., refreshing the page causing issues with selected items). I want to achieve this selective checkbox function in my cart page. Im using woocommerce and woodmart theme. Please advice on what to do
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值