开发者

Problem when changing text on input text when user write somethings

I have this code :

<input type="text" id="myText" class="input_box_hidden" />

$(document).ready(function() {
    var nomeAzienda=$("#myText");

    if(nomeAzienda.val()=="") nomeAzienda.val("Nome Azienda");
    if(nomeAzienda.val()!="Nome Azienda") nomeAzienda.removeClass().addClass("input_box");

    $("#myText").focus(function() {
        if(nomeAzienda.hasClass("input_box_hidden")) 
        {
            nomeAzienda.val("");
            nomeAzienda.removeClass().addClass("input_box");
        }
    });

    $("#myText").blur(function() {
        if(nomeAzienda.val()=="") 
        {
            nomeAzienda.val("Nome Azienda");
            nomeAzienda.removeClass().addClass("input_box_hidden");
        }
    });               
});

It works perfectly.

The problem is that : when I submit the input box to the server, I store the value when the client reply (so the user should not insert the value another time).

With this control :

    if(nomeAzienda.val()!="Nome Azienda") nomeAzienda.removeClass().addClass("input_box");

I check it's not the default value.

But what's if the user insert "Nome Azienda" as value? It doesnt works.

How can开发者_如何学JAVA I prevent this?


..then you're stuffed. but seriously, i think it's perfectly fine, you never know, there might be a company called "Company name". At least I've seen this problem everywhere textbox watermarks were used, so your solution is perfectly acceptable.


Not sure I understand, but if you want the filed to get the input_box_hidden class when the user inputs 'Nome Azienda', then you can add this to the blur function, right after the first if statement:

else if(nomeAzienda.val().toLowerCase()=='nome azienda')
{
   nomeAzienda.val("Nome Azienda");//correct char case for user input
   nomeAzienda.removeClass().addClass("input_box_hidden");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜