【问题标题】:How do I do different maxlengths for different text fields?如何为不同的文本字段设置不同的最大长度?
【发布时间】:2012-11-10 16:50:35
【问题描述】:

我的 WordPress 主题中有一个选项面板,其中包含多个文本字段,例如项目名称、项目描述等

case 'text':
        $val = $value['std'];
        $std = get_option($value['id']);
        if ( $std != "") { $val = $std; }
        $output .= '<input class="" maxlength="12" name=""'. $value['id'] .'" id="'. $value['id'] .'" type="'. $value['type'] .'" value="'. $val .'" />';
    break;

如上面的代码所示...我目前将文本字段的最大长度设置为 12,但我希望每个文本字段的最大长度都不同。我该怎么做?

【问题讨论】:

  • 在您定义输入名称的代码中存在语法错误。你有一个额外的报价 " 这里:name=" " '.$value['id'].' ", 这应该是 name="'.$value['id'].' "

标签: textfield maxlength


【解决方案1】:

定义一个包含字段及其对应的最大长度值的数组。 (我使用了字段名称,但您也可以使用 ID 号)

例如: field_name => max_length_for_that_field

$maxFieldLengths = array(
    'project_title' => 12,   //project title has 12 max length
    'project_description' => 100   //project description has 100 max length
);

确定您当前正在构建的文本字段(输入)并使用该标识符来引用您的 maxFieldLengths 数组。

case 'text':
    ...
    $field_name = // Identify the current field here;
    $output .= '<input class="" maxlength="'. $maxFieldLengths[$field_name] .'" etc .....' />
break;  

注意:从您的代码示例中,您无法 100% 清楚地识别您当前正在构建的字段。例如,您可以使用以下内容:

...
$field_name = getFieldFromId($value['id']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多