动态复选框名称以指定其值

时间:2011-05-06 03:21:28

标签: javascript

这段代码有什么问题我得到一个未定义的错误。我的复选框不是前端的数组,它使用不同的名称,我希望用户只选择一个复选框:

function select_item(index){
  var choice_options = [];
  choice_options['S'] = 'item_cb_S';
  choice_options['T'] = 'item_cb_T';
  choice_options['Z'] = 'item_cb_Z';
  choice_options['D'] = 'item_cb_D';
  choice_options['N'] = 'item_cb_N';


  for (i in choice_options) {
      var vl = choice_options[i];

      if(vl.substring(8) == index) {
           document.wizardform.choice_options[index].checked = true;    
           //alert("true");
      }
      else {
        document.wizardform.vl.checked = false; 
      }
  }
  return true;
}

1 个答案:

答案 0 :(得分:0)

它不是那么漂亮,但你可以使用eval ......

function select_item(index){
  var choice_options = [];
  choice_options['S'] = 'item_cb_S';
  choice_options['T'] = 'item_cb_T';
  choice_options['Z'] = 'item_cb_Z';
  choice_options['D'] = 'item_cb_D';
  choice_options['N'] = 'item_cb_N';


  for (i in choice_options) {
      var vl = choice_options[i];

      if(vl.substring(8) == index) {
           eval("document.wizardform." + choice_options[index] + ".checked = true;");
           //alert("true");
      }
      else {
        document.wizardform.vl.checked = false; 
      }
  }
  return true;
}