简体   繁体   English

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

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

Below is the simple requirement in html, jQuery, servlet下面是 html、jQuery、servlet 中的简单需求

Implementation: Forgot password module实现:忘记密码模块

  1. Username text field and send button.用户名文本字段和发送按钮。 --> OK --> 好的
  2. User enters username and press send button.用户输入用户名并按发送按钮。 --> Ok --> 好的
  3. Fire jquery on click event, post method --> OK在单击事件上触发 jquery,发布方法 --> 确定
  4. From DB get the security question for username --> OK从 DB 获取用户名的安全问题 --> 确定
  5. Get result in jquery call --> OK在 jquery 调用中获取结果 --> OK
  6. display security question value in text field --> NOK在文本字段中显示安全问题值 --> NOK

Some how I feel, the text field is updated and refreshed to old value.我的感觉是,文本字段已更新并刷新为旧值。 so in my case,所以就我而言,

step 1) Text value - placeholder property value步骤 1) 文本值 - 占位符属性值

step 2) update from jQuery第 2 步)从 jQuery 更新

step 3) again refreshed to placeholder property value步骤 3) 再次刷新为占位符属性值

jQuery 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 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">

Entire html code整个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