1

I have text boxes with default values, I am not able to enter values into them when i am trying to enter, I have to first delete the existing value using delete key then only i can enter.

I want to enter values and on tab change it should change for the other box as well, currently it is changing focus but I cannot enter values into them.

<input type="text" name="namebox" id="{concat('textboxvalue', position())}" value="{@proj}" style="font-size:10px;padding:0px;height:15px;width:90px;text-align:right;"/>

@proj: values coming from database

Thanks, Ani

4

4 回答 4

5

您可以在输入标签和 textarea 标签中使用占位符:

<input type='text' placeholder='Default text here' />
<textarea placeholder='Default text here'></textarea>

然后使用 jQuery 回退:

$(document).ready(function(e){
    var defaultVal = "Default text here";
    $("input, textarea").val(defaultVal).on("focus", function(){
        if($(this).val() == defaultVal){
            $(this).val("");
        }
    }).on("blur", function(){
        if($(this).val() == ""){
            $(this).val(defaultVal);
        }
    });

});

这里:http: //jsfiddle.net/EZexQ/1/

于 2013-07-25T09:58:57.973 回答
0

I would suggest you to have a look at this css-trick article.
It has an example with html5 placeholder attribute with jquery fallback.

function elementSupportsAttribute(element, attribute) {
  var test = document.createElement(element);
  if (attribute in test) {
    return true;
  } else {
    return false;
  }
};  


if (!elementSupportsAttribute('textarea', 'placeholder')) {
  // Fallback for browsers that don't support HTML5 placeholder attribute
  $("#example-three")
    .data("originalText", $("#example-three").text())
    .css("color", "#999")
    .focus(function() {
        var $el = $(this);
        if (this.value == $el.data("originalText")) {
          this.value = "";
        }
    })
    .blur(function() {
      if (this.value == "") {
          this.value = $(this).data("originalText");
      }
    });
} else {
  // Browser does support HTML5 placeholder attribute, so use it.
  $("#example-three")
    .attr("placeholder", $("#example-three").text())
    .text("");
}
于 2013-07-25T10:02:55.580 回答
0

On the focus. Just remove the values. Following is sample code in jquery. Can be done with plain javscript though.

<input value=10>
<input value=20>
<input value=30>

$('input').on('focus', function(){
    $(this).val('')
})
于 2013-07-25T10:02:59.710 回答
0

输入元素有一个名为defaultValue的 DOM 属性。我给出了这个解决方案,因为占位符在旧版浏览器中不起作用。

First name: <input type="text" name="fname" value="John"><br>
Last name: <input type="text" name="lname" value="Doe"><br>

在你的 jQuery 中,它可以被访问prop('defaultValue') 所以试试这个。

$('input').focus(function(){
 if($(this).val() == $(this).prop('defaultValue'))
    $(this).val('');
}).blur(function(){
    if($(this).val() == '')
    $(this).val($(this).prop('defaultValue'));
});

在这里工作小提琴

于 2013-07-25T10:22:15.357 回答