简体   繁体   English

带复选框的JavaScript操作

[英]JavaScript operation with checkboxes

I have group of check boxes and i need when checkboxes selected or deselected then local variable 'address' containing values of checkboxen but in my format. 我有一组复选框,当需要选中或取消选中复选框时,我需要包含复选框的值但采用我的格式的局部变量“地址”。 Sample: 样品:

http://localhost/?param=1&cb[gr1]=vl1-vl3&cb[gr2]=vl1&cb[gr3]=vl1-vl2-vl3

If i select Group 1 - vl1 and vl3 Group 2 - vl1 Group 3 - vl1 and vl2 and vl3 如果我选择组1-vl1和vl3组2-vl1组3-vl1和vl2和vl3

http://localhost/?param=1
&cb[gr1]=vl1-vl3
&cb[gr2]=vl1
&cb[gr3]=vl1-vl2-vl3

How to make such a miracle in Java script? 如何在Java脚本中创造这样的奇迹?

And this source of html 而这个HTML源

<form action="form.php" method="post">
    <fieldset>
        <legend>Group 1</legend>
        <input type="checkbox" name="gr1[]" value="vl1"/>value 1<br/>
        <input type="checkbox" name="gr1[]" value="vl2"/>value 2<br/>
        <input type="checkbox" name="gr1[]" value="vl3"/>value 3<br/>
    </fieldset>

    <fieldset>
        <legend>Group 2</legend>
        <input type="checkbox" name="gr2[]" value="vl1"/>value 1<br/>
        <input type="checkbox" name="gr2[]" value="vl2"/>value 2<br/>
    </fieldset>

    <fieldset>
        <legend>Group 3</legend>
        <input type="checkbox" name="gr3[]" value="vl1"/>value 1<br/>
        <input type="checkbox" name="gr3[]" value="vl2"/>value 2<br/>
        <input type="checkbox" name="gr3[]" value="vl3"/>value 3<br/>
    </fieldset>
</form>

https://jsfiddle.net/3vqr1ksy/ https://jsfiddle.net/3vqr1ksy/

To get these strings on the clientside, do this: 要在客户端获取这些字符串,请执行以下操作:

 [...document.querySelectorAll('fieldset')].forEach(function(fs) { fs.addEventListener('change', function(event) { let checkedValues = [...this.querySelectorAll(':checked')].map(cb => cb.value).join('-') window[this.dataset.group].textContent = checkedValues }) }) 
 fieldset { float: left; width: 27%; } 
 <form action="form.php" method="post"> <fieldset data-group="g1"> <legend>Group 1</legend> <label><input type="checkbox" name="gr1[]" value="vl1" />value 1</label><br/> <label><input type="checkbox" name="gr1[]" value="vl2" />value 2</label><br/> <label><input type="checkbox" name="gr1[]" value="vl3" />value 3</label><br/> </fieldset> <fieldset data-group="g2"> <legend>Group 2</legend> <label><input type="checkbox" name="gr2[]" value="vl1" />value 1</label><br/> <label><input type="checkbox" name="gr2[]" value="vl2" />value 2</label><br/> </fieldset> <fieldset data-group="g3"> <legend>Group 3</legend> <label><input type="checkbox" name="gr3[]" value="vl1" />value 1</label><br/> <label><input type="checkbox" name="gr3[]" value="vl2" />value 2</label><br/> <label><input type="checkbox" name="gr3[]" value="vl3" />value 3</label><br/> </fieldset> </form> <div>Group 1: <span id="g1"></span></div> <div>Group 2: <span id="g2"></span></div> <div>Group 3: <span id="g3"></span></div> 

To resolve this issue you need to get param, then check needed value dependance of your param, check this example: 要解决此问题,您需要获取参数,然后检查参数所需的值依赖性,请检查以下示例:

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = "param=1&cb[gr1]=vl1-vl3&cb[gr2]=vl1&cb[gr3]=vl1-vl2-vl3",//window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
};

var paramNum = getUrlParameter('param');
var paramGr  = getUrlParameter('cb[gr' + paramNum + ']');
var paramGrSplit = paramGr.split('-');

var valueString = '';
for(var i = 0; i < paramGrSplit.length; i++){
    $("input[value=" + paramGrSplit[i] + "]").prop('checked', true);
}

Live Demo 现场演示

When you have like this issue you need to subtract issue to subs and then create code for each sub, this will help you to resolve issue like this in future. 当您遇到这样的问题时,您需要将问题减去到子项中,然后为每个子项创建代码,这将有助于您将来解决此类问题。

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

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