开发者

Will this code work? Javascript/asp.net (Code not working)

开发者 https://www.devze.com 2022-12-14 21:37 出处:网络
<td class=\"style4\"> <input type=\"text\" ID=\"txtFirstName\" runat=\"server\" maxlength=\"50\"
        <td class="style4">
            <input type="text" ID="txtFirstName" runat="server" maxlength="50"
            class="DefaultTextbox" style="width:180px;" value="First Name" 
            onfocus="if(this.value=='First Name') {this.value = '';document.getElementById('spanFirstName').visible=false;}"
            onblur="if(this.value=='') this.value = 'First Name'"
            />
        </td>
        <td>
             <span id="spanFirstName">Should be less than 50 characters.</span>
        </td>

I want to do someth开发者_高级运维ing like this

onfocus = "if(this.value=='First Name') 
{
    this.value = '';
    document.getElementById('spanFirstName').visible=false;
}"
onblur = "if(this.value=='') this.value = 'First Name'"


I think you are looking for a TextBoxWatermark. Here is a nice one

Create Textbox Watermark Using CSS and JavaScript

For your code you can do like this

<script type='text/javascript'>
function HideSpan(elem)
{
    if ( elem.value == 'First Name' )
    {
        elem.value = '';
        document.getElementById("spanFirstName").style.visibility = 'hidden';   
    }   
}

function SetText(elem)
{
    if ( elem.value == '' )
    {
        elem.value = 'First Name';
            document.getElementById("spanFirstName").style.visibility = 'visible';
    }   
}

</script>
<table>
<tr>
      <td class="style4">
            <input type="text" ID="txtFirstName" runat="server" maxlength="50"
            class="DefaultTextbox" style="width:180px;" value="First Name" 
            onfocus="HideSpan(this);" onblur="SetText(this);"
            />
        </td>
        <td>
             <span id="spanFirstName">Should be less than 50 characters.</span>
        </td>
</tr>
</table>


I don't know asp.net but after looking at your code at this line:

document.getElementById('spanFirstName').visible=false;

I think it can be replaced with below line:

document.getElementById('spanFirstName').style.visibility='hidden';

Not sure, but that is how we do it, and that can be possibly the cause of the issue.

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号