簡體   English   中英

無法通過 jQuery 更新輸入類型文本值

[英]Input type text value cannot be updated via jQuery

下面是 html、jQuery、servlet 中的簡單需求

實現:忘記密碼模塊

  1. 用戶名文本字段和發送按鈕。 --> 好的
  2. 用戶輸入用戶名並按發送按鈕。 --> 好的
  3. 在單擊事件上觸發 jquery,發布方法 --> 確定
  4. 從 DB 獲取用戶名的安全問題 --> 確定
  5. 在 jquery 調用中獲取結果 --> OK
  6. 在文本字段中顯示安全問題值 --> NOK

我的感覺是,文本字段已更新並刷新為舊值。 所以就我而言,

步驟 1) 文本值 - 占位符屬性值

第 2 步)從 jQuery 更新

步驟 3) 再次刷新為占位符屬性值

jQuery

$(document).on("click", "#btnuserName", function() {        
    $.post("/zmcwebadmin/ForgotPasswordServlet",function(securityQuestion) {     
        alert("I got the response in ajax "+ securityQuestion);//value is correct 
        $("input[type=text].txt_securityQuestion").val(securityQuestion);  //problem here
        console.log("txt_securityQuestion");    
    });
    });

html

<input type="text" id="ForgotPassUname" name="user_name" class="changepassformat"placeholder="Enter Username">

<button class="buttonformat" id="btnuserName"   name="btnuserName">SEND</button><br>

<input type="text" id="txt_securityQuestion" name="txt_securityQuestion" class="changepassformat" placeholder="Security Question"><br>

<input type="text" id="SecurityAns" name="SecurityAns" class="changepassformat" placeholder="Enter the Answer">

整個html代碼

<body background="../Images/zebra_background.jpg">

<div id="header">
    <span style="float: left">ZMC Server </span> <img
        src="../Images/zebra_logo.png" width=150px height=50px
        style="float: right; padding-top: 5px">
    <form id="form_logout">
        <input type="image" class="logbuttonformat" id="logoutbtn"
        src="../Images/logout_deselected.png" onclick="changeLogoutImage()"
        alt="submit" style="padding: auto">
    </form>
</div>
<form  id="form_forgotpswd" >
<p  class="slectedNameformat"> FORGOT PASSWORD </p>
<input type="text" id="ForgotPassUname" name="user_name" class="changepassformat"placeholder="Enter Username">
<button class="buttonformat" id="btnuserName" name="btnuserName">SEND</button><br>
<input type="text" id="txt_securityQuestion" name="txt_securityQuestion" class="changepassformat">
<br>
<input type="text" id="SecurityAns" name="SecurityAns"class="changepassformat" placeholder="Enter the Answer">
<br><input type="button" class="buttonformat" value="SUBMIT">
</form>
</body>
</html>

您需要 id 選擇器而不是類選擇器,因為txt_securityQuestion是元素的 id 而不是它的類:

 $("#txt_securityQuestion").val(securityQuestion);  

暫無
暫無

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

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