2015-12-10 30 views
0

得到可變元素沒有客戶端Id我想在「文本」變量因素沒有客戶端Id我想在javascript

 function fncInputDateTimeOnly() { 
    var textBox = document.getElementById("MainContent_txtFilingStartDate"); 
        var textLength = textBox.value.length; 
        if (textLength >= 10) { 
         event.returnValue = false; 
        } 

    <asp:TextBox ID="txttDate" onkeypress="fncInputDateTimeOnly()" runat="server" Width="195px" /> 

上面的代碼是可以正常使用,但我想做到這一點,它不工作

function fncInputDateTimeOnly() { 
     var textBox = this; //how to get the element in variable 
         var textLength = textBox.value.length; 
         if (textLength >= 10) { 
          event.returnValue = false; 
         } 
+0

真不明白。什麼不起作用?你想實現什麼? – Trilarion

回答

1

通過this作爲該方法的參數。

<asp:TextBox ID="txttDate" onkeypress="fncInputDateTimeOnly(this)" runat="server" Width="195px" /> 
function fncInputDateTimeOnly(textBox) { 
    var textLength = textBox.value.length; 
    ... 
+0

完成謝謝:-) – zulqarnainjalil