0

フォームをクリアし、入力ヒントを使用するための2つのスクリプトがあります。clear関数を実行した後、入力ヒントが返されません。

function clear_form_elements(ele) {

$(ele).find(':input').each(function() {
switch(this.type) {
    case 'password':
    case 'select-multiple':
    case 'select-one':
    case 'text':
    case 'textarea':
        $(this).val('');
        $("  .bx4a").removeAttr("style");
        $("  .bxTXTa").removeAttr("style");
        $("  .bxTXTa2").removeAttr("style");
        $("  .bx4a2").removeAttr("style");
        $("  .bx").removeAttr("style");
        $("  .ts").removeAttr("style");
        $("  .button-toggle-on").removeAttr("style");
        $("  .gnM").removeAttr("style");
        $("  .gnF").removeAttr("style");
        break;
    case 'checkbox':
    case 'radio':
        this.checked = false;
   }
  });

 }






 // jQuery Input Hints plugin
 // Copyright (c) 2009 Rob Volk
 // http://www.robvolk.com

jQuery.fn.inputHints=function() {
// hides the input display text stored in the title on focus
// and sets it on blur if the user hasn't changed it.

// show the display text
$(this).each(function(i) {
    $(this).val($(this).attr('title'))
        .addClass('hint');
});

// hook up the blur & focus
return $(this).focus(function() {
    if ($(this).val() == $(this).attr('title'))
        $(this).val('')
            .removeClass('hint');
}).blur(function() {
    if ($(this).val() == '')
        $(this).val($(this).attr('title'))
            .addClass('hint');
});
};

$(document).ready(function() {$('input[title],textarea[title]').inputHints();});
4

2 に答える 2

0

ヒントテキストの戦略は非常に単純です。入力のデフォルト値を目的のヒントテキストに設定し、フォーカスが取得されたら値を''に設定します。次に、onblurで、値がまだ''の場合は、値をデフォルト値に設定します。例えば

<label for="userName">Name:<input name="userName" id="userName"
 value="Your full name&hellip;" class="hint"></label>

<script>
function hintFocus() {
  if (this.value == this.defaultValue) this.value = '';
}
function hintBlur(el) {
  if (this.value == '') this.value = this.defaultValue;
}

// Add the listeners
var el = document.getElementById('userName');
el.onfocus = hintFocus;
el.onblur = hintBlur;
</script>

フォームの標準のリセットボタンを使用してリスナーを指定し、フォームがリセットされたときに入力タイプのテキストとテキストエリア(必要に応じて)にヒントクラスを追加します。

于 2012-04-14T03:03:41.207 に答える
0

RobGの回答にはバグがあると思います。ユーザーがヒント(フルネーム…)とまったく同じテキストを入力し、どこかをクリックして入力をクリックすると、ユーザーが入力した内容が表示されなくなります。

于 2014-04-21T10:24:49.717 に答える