簡體   English   中英

當文本框不為null /一些值傳遞到文本框時,如何從文本框調用javascript函數

[英]How to call a javascript function from a textbox when the textbox is not null / some value is passed into the textbox

我遇到的情況是,我有一個文本框,該文本框將更新為某些值,並且一旦文本框獲得其值,就會調用javascript函數。 我嘗試了一些東西,但是這不起作用

if (validSubmission) {
    User user = new User(); 
    String StatusMessage=user.executeUserTask("_create_","",userBusinessEmailId,userPassword,cityOfBirth,userFirstName,userLastName,userCompanyName,userCompanyAddress,userPhone,"");
    if(StatusMessage.equalsIgnoreCase("OK")) {
        response.sendRedirect("login.jsp?retMessage="+"Account created sucessfully");
    }
    else {
        //response.sendRedirect("login.jsp?retMessage="+StatusMessage);
        responseMsg = "Invalid Domain Entry";
        {%>
            Redirect();
        <%}
    }
}

這是我獲得價值的文本框

<input type="text" id="keyToShowInvalidDomain" name="keyToShowInvalidDomain" value="<%=responseMsg%>" onchange="Redirect()"> 

這是我要調用的功能

<script type="text/javascript">
    function Redirect() {
        alert($("#keyToShowInvalidDomain").val());
    }
</script>

有人可以幫忙嗎?

對於輸入類型,文本onchange事件將不起作用,請嘗試使用onkeyuponkeydown

<input type="text" id="keyToShowInvalidDomain" name="keyToShowInvalidDomain" value="<%=responseMsg%>" onkeyup="Redirect()"> 

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> function Redirect() { alert($("#keyToShowInvalidDomain").val()); } </script> </head> <body> <input type="text" id="keyToShowInvalidDomain" name="keyToShowInvalidDomain" value="<%=responseMsg%>" onchange="Redirect()"> </body> </html> 

您正在使用基於jQuery的選擇器,因此您需要向頁面添加jQuery庫。 請嘗試這個。

$(document).ready(function() {
   $('#keyToShowInvalidDomain').change(function(){
        // Call redirect function
    });
}

上面的代碼應與JQuery一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM