0

我刚刚开始使用Phonegap,我想在用户单击文本框时清除文本框内容。

HTML:

<input type="text" class="clear" id="dateVal" name="date" value="date" onblur="clear();"/>/

JavaScript

function clear() {
        document.getElementsByTagName('input').value = '';
    }

但是 clear 函数没有被调用。另外,只是尝试alert输入clear() 功能(没有帮助)。其他一切正常。任何帮助,将不胜感激。

完整的 HTML 代码:

<!DOCTYPE html> <html>   <head>
    <title>Age Calculator</title>

    <script type="text/javascript" charset="utf-8" src="cordova-1.8.1.js"></script>
    <script type="text/javascript" charset="utf-8">
      document.addEventListener("deviceready",onDeviceReady,false);

    function onDeviceReady() { 
        alert('welcome');
    }
        function calAge() {         
          var x = confirm('Click here to calculate the age'); 
          if(x == true) {
           document.getElementById('ageId').style.display = block';
          } else {
              navigator.app.exitApp();      }
         }   
        function submitValues() {
                var todaysDate = new Date();
                var y = todaysDate.getFullYear();
                var m = todaysDate.getMonth() + 1;
                var d = todaysDate.getDate() + 1;
                var myYear = document.getElementById('yearVal').value;
                var myMonth = document.getElementById('monthVal').value;
                var myDate = document.getElementById('dateVal').value;
                var myYear = (y-myYear);
                var myMonth = (m-myMonth);
                var myDate = (d-myDate);
                document.getElementById('ageId').style.display = 'none';    
                document.getElementById('result').innerHTML = 'You are '+myYear+'years '+myMonth+' months and '+myDate + ' days old :-)';
                            }   function clear() {      document.getElementsByTagName('input').value = '';  }
    </script>   </head>   <body>
    <button onclick="calAge();">Age Calculator</button> <br>
    <div id="ageId" style="display:none;">
        <b>Please Enter your Date Of Birth in (dd/mm/yyyy) format:</b>
        <input type="text" class="clear" id="dateVal" name="date" value="date" onblur="clear();"/>/
        <input type="text" class="clear" id="monthVal" name="month" value="month" />/
        <input type="text" class="clear" id="yearVal" name="year" value="year" />
        <input type="button" value="submit" onclick = "submitValues();" />
    </div>
    <div id="result">
    </div>   </body> </html>
4

1 回答 1

0

在 HTML5 中有一个placeholder属性。前任:

<input type="text" placeholder="Enter Date" id="dateVal" name="date" />

我们可以用它来得到我想要的。谢谢,可能对某人有帮助。

于 2012-06-28T07:27:27.663 回答