一段javascript代码的说明

时间:2015-07-03 08:56:26

标签: javascript php web

我是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;
  }

请帮助我了解它可能在做什么。也许有些方向。我不确定这段代码是否足够,请尽力帮助我,并尽可能向我解释。非常感谢您的帮助。

2 个答案:

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

查看您发布的代码片段,然后它就完全没有了

希望有所帮助

答案 1 :(得分: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;
}