简体   繁体   English

使用JavaScript在Struts2文本字段中的占位符

[英]Placeholder in Struts2 textfield using javascript

Good day! 美好的一天!

I am trying to create a placeholder on my Struts2 Textfield. 我试图在我的Struts2文本字段上创建一个占位符。 The problem is, there is no built-in attribute for it. 问题是,没有内置属性。 So i've decided to use javascript. 所以我决定使用javascript。 But I don't know how I can effect it on the struts text field. 但是我不知道如何在struts文本字段上实现它。

Code for html textfield placeholder in javascript is as follow: javascript中html文本字段占位符的代码如下:

HTML: <input type="text" name="billingNameStreet" class="input" title="Street"/> HTML: <input type="text" name="billingNameStreet" class="input" title="Street"/>

   <script type="text/javascript">
        $(':input[title]').each(function() {
            var $this = $(this);
            if($this.val() === '') {
                $this.val($this.attr('title'));
            }
            $this.focus(function() {
                if($this.val() === $this.attr('title')) {
                    $this.val('');
                }
            });
            $this.blur(function() {
                 if($this.val() === '') {
                     $this.val($this.attr('title'));
                 }
            });
        });
    </script> 

How can i effect it on my struts2 textfield: 我如何在我的struts2文本字段上实现它:

<s:textfield label="TEST" name="test" cssClass="haha"/>
<s:textfield label="TEST1" name="test1" cssClass="haha1"/>
<s:textfield label="TEST2" name="test2" cssClass="haha2"/>

Or is there other way to create a placeholder in Struts2 textfield? 还是有其他方法在Struts2文本字段中创建占位符?

Thanks in advance 提前致谢

Here your jQuery code says that the selector is input elements with title attribute. 在这里,您的jQuery代码说选择器是具有title属性的input元素。 In your struts2 tags, the title is not specified. 在您的struts2标记中,未指定标题。 In <s:textfield/> tag, you can use the title attribute which will be rendered to html title attribute. <s:textfield/>标记中,您可以使用title属性,该属性将呈现为html title属性。 Hence, your code would need to be changed to something like: 因此,您的代码将需要更改为以下内容:

<s:textfield label="TEST" name="test" cssClass="haha" title="haha"/>
<s:textfield label="TEST1" name="test1" cssClass="haha1" title="haha1"/>
<s:textfield label="TEST2" name="test2" cssClass="haha2" title="haha2"/>

You can use this document for future references: 您可以将该文档用作将来的参考:

http://struts.apache.org/2.0.14/docs/textfield.html http://struts.apache.org/2.0.14/docs/textfield.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM