我有一个问题,我不明白。创建动态复选框Javascript
我正在基于数组创建复选框的“生成器”。
\t \t \t var array = new Array();
\t \t \t array[0]="Monday";
\t \t \t array[1]="Tuesday";
\t \t \t array[2]="Wendesday";
\t \t \t array[3]="Thirsday";
\t \t \t array[4]="Friday";
\t \t \t array[5]="Saturday";
\t \t \t array[6]="Sunday";
\t \t \t var cbh = document.getElementById('checkboxes');
\t \t var val = '';
\t \t var cap = '';
\t \t var cb = document.createElement('input');
\t \t var j = "";
\t \t \t for (var i in array) {
\t \t \t \t //Name of checkboxes are their number so I convert the i into a string.
\t \t \t \t j = i.toString();
\t \t \t \t console.log('J = ', j);
\t \t \t \t val = j;
\t \t \t \t //cap will be the value/text of array[i]
\t \t \t \t cap = array[i];
\t \t \t \t console.log('cap =', cap);
\t \t \t cb.type = 'checkbox';
\t \t \t cbh.appendChild(cb);
\t \t \t cb.name = val;
\t \t \t cb.value = cap;
\t \t \t cbh.appendChild(document.createTextNode(cap));
\t \t \t }
\t \t \t * {
\t \t \t box-sizing: border-box;
\t \t \t }
\t \t \t #data {
\t \t \t padding:0;
\t \t \t \t width:100vw;
\t \t \t }
\t \t \t .multiselect {
\t \t \t \t overflow: visible;
\t \t \t \t padding:0;
\t \t \t \t padding-left:1px;
\t \t \t \t border:none;
\t \t \t \t background-color:#eee;
\t \t \t \t width:100vw;
\t \t \t \t white-space: normal;
\t \t \t \t height:200px;
\t \t \t }
\t \t \t .checkboxes {
\t \t \t \t height:100px;
\t \t \t \t width:100px;
\t \t \t \t border:1px solid #000;
\t \t \t \t background-color:white;
\t \t \t \t margin-left:-1px;
\t \t \t \t display:inline-block;
\t \t \t }
\t \t </style>
<form>
\t \t \t <div id="data">
\t \t \t \t <div class="multiselect">
\t \t \t \t \t <div id="checkboxes">
\t \t \t \t \t </div>
\t \t \t \t </div>
\t \t \t </div>
\t \t </form>
的问题是,它将只创建一个checkboxe,最后一个元素,我不明白为什么...我想每天都有一个复选框。
之后,我问如何检索元素选择,但这是在第二部分。
所以,如果有人有一个想法,我会很感激!
关于。
你不需要j = i.toString( )。每当你做“somestring”+我,引擎正在为你做... –