【问题标题】:Form text diappears and comeing ab up again表单文本消失并再次出现
【发布时间】:2012-01-19 11:44:24
【问题描述】:

我如何在 symfony 中做到这一点?

http://dorward.me.uk/tmp/label-work/example.html

当我点击输入字段用户名时。文字消失。如果我不输入任何内容并单击其他地方,文本将再次出现。我如何在 lib/forms/ 中的 symofny 中做到这一点。

感谢您的建议!

寻宝者

【问题讨论】:

    标签: forms symfony1 symfony-1.4


    【解决方案1】:

    这不是 symfony - 它的 javascript ... 看看源代码(使用 jQuery)

    HTML:

    <div class="slim-control">
        <label for="username"> Username </label>
        <input name="username" id="username">
    </div>
    <div class="slim-control">
        <label for="password"> Password </label>
        <input name="password" id="password" type="password">
    </div>
    

    JavaScript:

    (function () {
        var nonEmpty = "non-empty";
        var inputs = jQuery('.slim-control input');
        var setLabelStyle = function setLabelStyle () {
            var label = jQuery(this);
            if (label.val().length) {
                label.addClass(nonEmpty);
            } else {
                label.removeClass(nonEmpty);
            }
        };
        inputs.focus(function () {
            jQuery(this).addClass(nonEmpty);
        });
        inputs.blur(setLabelStyle);
        inputs.each(setLabelStyle);
    }());
    

    因此,如果您想这样做 - 例如,您需要将 javascript 添加到模板 (indexSuccess.php) - 您可能需要修改 ids / classes 以满足您的本地需求

    更新

    您可以创建以下表单 - 这将与上述表单匹配(未经测试):

    $this->form = new sfForm();
    $this->form->setWidgets(array(
        'username' => new sfWidgetFormInputText(),
        'password' => new sfWidgetFormInputPassword()
    ));
    

    创建表单(实际上是所有 Symfony)在这里都有很好的记录 -> http://www.symfony-project.org/gentle-introduction/1_4/en/10-Forms

    【讨论】:

    • 你能把 lib/form/form.php 也发给我吗?那很好啊!无论如何谢谢!
    • @craphunter 我已经添加了在操作中创建表单所需的代码
    • 是的!非常感谢您提供非常快速且有效的答案!再次感谢!
    【解决方案2】:
    'input' => new sfWidgetFormInputText(array(), array('size' => '100', 'maxlength' => '255', 'value' => 'Some text', 'onblur' =>"if(this.value == &#039;&#039;) { this.value=&#039;Some text&#039;}", 'onfocus' =>"if (this.value == &#039;Some text&#039;) {this.value=&#039;&#039;}")),
    

    这要短得多!注意“某些文本”是相同的!

    如果您需要传递更多信息,请查看 Symfony 形式的 SetWidgetsHtml:(向下滑动) http://www.symfony-project.org/forms/1_4/en/01-Form-Creation

    【讨论】:

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