0

我想要一个启用/禁用文本字段的复选框。启用复选框和文本字段后,获取文本字段的值并相应地设置下拉选项的数量,

例如:如果文本字段的值为 4,则下拉选项应为 1 2 3 4。

我有以下代码。请帮我解决,谢谢。

Cakephp 表单

echo $this->Form->checkbox('custom_lvl_def', array('hiddenField' =>       false,'OnClick'=>"enable_disable()"));
echo "Use custom level definitions"."&nbsp";
echo $this->Form->input('alternative_no_of_levels', array('style'=>'width:70px','label'=>'levels','div'=>false, 'disabled'=>TRUE));
echo "<br>";

echo $this->Form->input('dropdown', array(
  'options'=>$options,
  'empty' => 'Select Level',
  'label'=>'Type:',
  'selected'=>'Select Level',
  'style'=>'width:130px',
  'div'=>false
));

JavaScript 函数

function enable_disable()
{        
    if($('#CompetenceCustomLvlDef').attr('checked'))
        {               
            $('#CompetenceAlternativeNoOfLevels').removeAttr("disabled");
            // var $a=array ('Level');
            var value = $('#CompetenceAlternativeNoOfLevels').val();
            for ( var i = 0; i < value; i++ ) {
                $('#CompetenceDropdown').options[i].selected = i;           
            return; }           
        }
    else                 
            $('#CompetenceAlternativeNoOfLevels').attr('disabled', true);

}
4

1 回答 1

1

这可能会帮助您获取您输入的级别数的下拉列表数组。根据问题,您可以在表格中尝试这个..

echo $this->Form->checkbox('custom_lvl_def', array('hiddenField' => false,'id'=>'id_custom_lvl_def', 'onclick'=>"javascript:enable_disable()"));
echo "Use custom level definitions"."&nbsp";
echo $this->Form->input('alternative_no_of_levels', array('style'=>'width:70px','id'=>'id_alternative_no_of_levels','label'=>'levels','onblur'=>'javascript:enable_disable();', 'div'=>false, ));
echo "<br>";
echo $this->Form->input('dropdown', array(
  'options'=>'',
  'empty' => 'Select Level',
  'label'=>'Type:',
  'type'=>'select',
  'id'=>'id_dropdown',
  'name'=>'dropdown',
  'style'=>'width:130px',
  'div'=>false
));

在脚本中:

function enable_disable()
{        
    $("#id_dropdown").empty();
    if($('#id_custom_lvl_def').attr('checked'))
    {               
        $('#id_alternative_no_of_levels').removeAttr("disabled");
        var value = $('#id_alternative_no_of_levels').val();
        var optionsAsString = "";
        optionsAsString += "<option value=''>" + "Select Level" + "</option>";
        for ( var i = 1; i <= value; i++ ) {
            optionsAsString += "<option value='" + i + "'>" + i + "</option>";
        }           
        $( 'select[name="dropdown"]' ).append( $( optionsAsString ) );
    }
    else                 
        $('#id_alternative_no_of_levels').attr('disabled', true);
}
于 2013-03-06T09:06:58.603 回答