using jquery focus to empty text fields

南楼画角 提交于 2020-01-15 12:43:06

问题


I am trying to add focus on the following code, which would loop through the 10 or more fields and remove the default value(s) when the user focuses on the field. how can i add the focus to the existing code? -thanks

$(document).ready(function() {  
    $('#tradition').click(function() {
    $('#form-container').attr("class","trade");                         
    $('.lblMadlib11').text("Eleven ");
    $('.lblMadlib11a').val("eleven value field");
});

回答1:


Assuming this markup this:

Html:

<input type="text" placeholder="default text" />

Javascript:

$(function(){ // onload
    $('input:text').focus(function(){ // onfocus
       if($(this).val() == $(this).attr('placeholder')) // if value is default value
           $(this).val(''); // clear value
    });
});


来源:https://stackoverflow.com/questions/4115909/using-jquery-focus-to-empty-text-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!