2015-07-03 33 views
-2

我是一名JavaScript初學者,我正在看下面這段代碼。一段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; 
    } 

請幫助給我一些它可能在做什麼的想法。也許有些方向。我不確定這段代碼是否足夠,請儘量幫助我,並儘可能地向我解釋。您的幫助將非常感謝。

+0

使用瀏覽器中的JavaScript調試器跟蹤代碼並檢查變量。然後你可以看看自己在做什麼 – RiggsFolly

+0

噢好的。有一種叫做javascript調試器的東西。謝謝 - RiggsFolly –

+0

無論如何,如何使用它,我仍然困惑。 –

回答

1

它建立一個選擇下拉框中的一些選項

<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> 

綜觀您發佈的代碼片斷,然後它確實有它

希望絕對沒有任何幫助

+0

如何使用JavaScript來製作帶有標籤的文本框並獲取輸入並將其存儲到數據庫中的特定列 –

+0

最好提出一個新問題 –

0
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; 
}