在文本框中显示标签的JavaScript问题

时间:2012-02-08 04:17:22

标签: javascript

您好我的输入类型文本定义如下

  <input type="text" name="dateInputUserActivity1" value="" id="dateInputUserActivity1" onfocus="HideLabel(this)" onblur="ShowLabel(this)">

我在外部javascript文件中定义了函数HideLabel(textbox)ShowLabel(textbox),如下所示:

          function HideLabel(txtField){
        if(
           txtField.name=='dateInputResourceEventActivityComparison1' 
            || txtField.name=='dateInputResourceActivityComparison1' 
            || txtField.name=='dateInputResourceEventStatistics1'
            || txtField.name=='dateInputUserImportHistory1'
            || txtField.name=='dateInputUserActivity1'
            || txtField.name=='dateInputUserEventStatistics1'
            || txtField.name=='dateInputUserStatistics1'   // top10distinct chart date
            || txtField.name=='dateInputtop10UserActivity1'
        ){
            if(txtField.value=='From Date')
                txtField.value = '';
            else
                txtField.select();
        }else if(txtField.name=='dateInputResourceEventActivityComparison2'
        || txtField.name=='dateInputUserActivity2'
        || txtField.name=='dateInputUserEventStatistics2'
        || txtField.name=='dateInputResourceActivityComparison2'
        || txtField.name=='dateInputUserImportHistory2'
        || txtField.name=='dateInputUserStatistics2'
        || txtField.name=='dateInputtop10UserActivity2'
        || txtField.name=='dateInputResourceEventStatistics2'){
            if(txtField.value=='To Date'){
                txtField.value = '';

            }
            else{

                txtField.select();
            }
        }
    } 

function ShowLabel(txtField){
        if(
               txtField.name=='dateInputResourceEventActivityComparison1' 
            || txtField.name=='dateInputResourceActivityComparison1' 
            || txtField.name=='dateInputResourceEventStatistics1'
            || txtField.name=='dateInputUserImportHistory1'
            || txtField.name=='dateInputUserActivity1'
            || txtField.name=='dateInputUserEventStatistics1'
            || txtField.name=='dateInputUserStatistics1'   // top10distinct chart date
            || txtField.name=='dateInputtop10UserActivity1'
){
            if(txtField.value.trim()=='')
                txtField.value = 'From Date';
        }else if(
            txtField.name=='dateInputResourceEventActivityComparison2'
        || txtField.name=='dateInputUserActivity2'
        || txtField.name=='dateInputUserEventStatistics2'
        || txtField.name=='dateInputResourceActivityComparison2'
        || txtField.name=='dateInputUserImportHistory2'
        || txtField.name=='dateInputUserStatistics2'
        || txtField.name=='dateInputtop10UserActivity2'
        || txtField.name=='dateInputResourceEventStatistics2'
    ){
            if(txtField.value.trim()==''){
                txtField.value = 'To Date';
                //txtField.type = 'text';
            }
        }
    }    

这些是我想要修改以在文本框中显示标签的所有日期字段文本框。

现在只有if语句中的第一个字段显示文本框中的标签,其他字段仅在我聚焦并聚焦在字段外时才显示标签。如何在pageload上的所有字段上显示标签?

2 个答案:

答案 0 :(得分:1)

您是否考虑过

<input type="text" name="dateInputUserActivity1" value="" id="dateInputUserActivity1" placeholder="From Date">

关键是placeholder属性。

答案 1 :(得分:0)

问题是设置默认值=“从日期”,这是空的..所以我在所有文本字段标签中添加了它并且工作正常。