简体   繁体   English

具有多个参数的复选框函数调用

[英]checkbox function call with multiple parameters

I have a simple checkbox along with label like below. 我有一个简单的复选框以及如下标签。

<input id="unreadCheck0" class="styled" type="checkbox" value="WSIO20DEMS131402">
<label for="unreadCheck0">
<a id="unread0" class="alert-link" href="javascript:loadSingleUnreadAdvisory('WSIO20DEMS131402',0,'201605','06',0);">WSIO20DEMS131402</a>
</label>

The whole code above will be dynamically inserted via javascript. 上面的整个代码将通过javascript动态插入。 Also there will be multiple check boxes like above. 同样,上面会有多个复选框。 My objective is to make two different function call on two events. 我的目标是对两个事件进行两个不同的函数调用。

First one, when I click check box label. 第一个,当我单击复选框标签时。 I have achieved this like above. 我已经实现了上述目标。

Second one, when I click the checkbox I need to call the same function with different parameters. 第二个,当我单击复选框时,我需要使用不同的参数调用相同的函数。 For example loadSingleUnreadAdvisory('WSIO20DEMS131402',1,'201605','06',0); 例如loadSingleUnreadAdvisory('WSIO20DEMS131402',1,'201605','06',0);

I can write a listener for all check boxes. 我可以为所有复选框编写一个侦听器。 But how to pass multiple parameters to that function? 但是如何将多个参数传递给该函数呢? I can send all values inside value property with delimiter. 我可以使用定界符在value属性内发送所有值。 Is there any other better approach to do this? 还有其他更好的方法吗?

You can use HTML5 data-* attributes. 您可以使用HTML5 data- *属性。 You could have all your data for any elements in data-* attributes. 您可以将所有数据包含在data- *属性中的任何元素中。

Something like below. 像下面这样。

 $(document).ready(function(){ $(".chkBoxLabel").on("click", function(evnt){ evnt.preventDefault(); console.log($(evnt.target).data("info")); var param1 = $(evnt.target).data("info") callMe(param1); }); $(".chkBox").on("click", function(evnt){ console.log($(evnt.target).data("fname")); console.log($(evnt.target).data("lname")); var param1 = $(evnt.target).data("fname") var param2 = $(evnt.target).data("lname") callMe(param1, param2); evnt.stopPropagation(); }); function callMe(){ // Based on number of params you can handle appropriately console.log(arguments.length); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label class="chkBoxLabel" data-info="label1"> <input class="chkBox" type="checkbox" data-fname="sandeep" data-lname="nayak"/> Chkbox1 </label> <label class="chkBoxLabel" data-info="label2"> <input class="chkBox" type="checkbox" data-fname="sample" data-lname="name"/> Chkbox2</label> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM