输入类型文本值无法通过jQuery更新

时间:2016-03-23 06:39:08

标签: javascript jquery html

以下是html,jQuery,servlet

中的简单要求

实施:忘记密码模块

  1. 用户名文本字段和发送按钮。 - >行
  2. 用户输入用户名并按发送按钮。 - >确定
  3. 点击事件,帖子方法 - >点击jquery行
  4. 从DB获取用户名的安全性问题 - >行
  5. 在jquery调用中获取结果 - >行
  6. 在文本字段中显示安全问题值 - > NOK
  7. 我感觉如何,文本字段已更新并刷新为旧值。 所以在我的情况下,

    步骤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>
    

1 个答案:

答案 0 :(得分:1)

你需要id选择器而不是类选择器,因为txt_securityQuestion是元素的id而不是它的类:

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