如何通过jquery将字符串first和last添加到复选框

这是我的html:


<input class="eret" type="checkbox" value="Red">Red</input>

<input class="eret" type="checkbox" value="Green">Green</input>

<input class="eret" type="checkbox" value="Blue">Blue</input>

<input class="eret" type="button" value="Get checkboxes" id="getCheckboxesButton"></input>

<div id="debugOutput">

</div>

这是我的 JavaScript:


$(document).ready(function() {

    $('#getCheckboxesButton').live('click', function(event) {

        var checkboxValues = [];

        $('input.eret[type="checkbox"]:checked').each(function(index, elem) {

            checkboxValues.push($(elem).val());

        });

        $('#debugOutput').html(checkboxValues.join(','));

    });

});

如何使用此输出定义 Javascript 代码?


输出(示例):


Color:"Green"  --> One : Without Comma


Color:"Green",Color:"Blue" / Color:"Red",Color:"Green",Color:"Blue"  --> Multi : With Comma

https://i.stack.imgur.com/iFyWT.png


jsfiddle


翻过高山走不出你
浏览 78回答 1
1回答

鸿蒙传说

像这样的东西吗?$(document).ready(function() {&nbsp; &nbsp; $('#getCheckboxesButton').on('click', function(event) {&nbsp; &nbsp; &nbsp; &nbsp; var checkboxValues = [];&nbsp; &nbsp; &nbsp; &nbsp; $('input.eret[type="checkbox"]:checked').each(function(index, elem) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkboxValues.push('Color:"'&nbsp; +&nbsp; $(elem).val() + '"');&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; $('#debugOutput').html(checkboxValues.join(','));&nbsp; &nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input class="eret" type="checkbox" value="Red">Red<input class="eret" type="checkbox" value="Green">Green<input class="eret" type="checkbox" value="Blue">Blue<input class="eret" type="button" value="Get checkboxes" id="getCheckboxesButton"><div id="debugOutput"></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5