[英]Explanation for a piece of javascript code
我是 javascript 的初學者,我正在查看以下代碼。
var type_select = '<select id="type_select" style="margin-bottom:0px;">';
var i;
var customer_group = <?php echo json_encode($customer_group);?>;
for (i = 0; i < customer_group.length; ++i) {
//console.log(customer_group[i].group_id);
if (customer_group[i].group_name == table_column_1){
type_select = type_select+'<option value='+customer_group[i].group_id+' selected>'+customer_group[i].group_name+'</option>';
}else{
type_select = type_select+'<option value='+customer_group[i].group_id+'>'+customer_group[i].group_name+'</option>';
}
}
type_select = type_select+'</select>';
//not allow to click header
if ( col == 0 ) {
return;
}
請幫助我了解它可能在做什么。 也許有方向。 我不確定這段代碼是否足夠,請盡力幫助我並盡可能多地向我解釋。 您的幫助將不勝感激。
它正在構建一個帶有一些選項的選擇下拉框
<select id="type_select" style="margin-bottom:0px;">
<option value="some-value">Some text</option>
<option value="some-other-value">Some other text</option>
<option value="yet-another-value" selected>More text this one is selected on load</option>
</select>
查看您發布的代碼片段,然后它完全沒有任何作用
希望有幫助
var type_select = '<select id="type_select" style="margin-bottom:0px;">';
var i;
// this part of code will be interpreted by PHP Engine, on client side you will find an JSON representation of
// "Customer_group", probably gotten from database, or file
var customer_group = <?php echo json_encode($customer_group);?>;
for (i = 0; i < customer_group.length; ++i) {
// here, you are building a HTML string, this will be attached to DOM using
// ex : document.getElementById('YOUR_DOM_ID').innerHTML = type_select;
if (customer_group[i].group_name == table_column_1){
type_select = type_select+'<option value='+customer_group[i].group_id+' selected>'+customer_group[i].group_name+'</option>';
}else{
type_select = type_select+'<option value='+customer_group[i].group_id+'>'+customer_group[i].group_name+'</option>';
}
}
type_select = type_select+'</select>';
//not allow to click header
if ( col == 0 ) {
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.