【问题标题】:jquery mobile: Change textarea background after submit button has been pressedjquery mobile:按下提交按钮后更改textarea背景
【发布时间】:2013-05-10 12:50:07
【问题描述】:

我想更改显示“登录成功!”的文本区域。或“登录失败!”在按下 HTML 表单的登录按钮后。按下登录按钮时,文本区域应淡入。

如果登录成功, 此文本区域的背景应更改为带有黑色字体的绿色并显示消息。

如果不成功,应该将文本区域更改为带有白色字体的红色并显示错误消息(带有错误处理程序)。

默认情况下,此文本区域是隐藏的。

请注意,无论是否找到用户/密码,我都会收到来自服务器的回调,但这将在稍后实现。因此,我只想在单击 login_submit 按钮时发送一个随机的“true”或“false”。

这是我的 HTML 代码:

<div data-role="content">

<form method="POST" data-ajax="false">
<label for="login_username">Username:</label>
<input type="text" name="login_username" id="login_username"/><br>

<label for="login_password">Passwort:</label>
<input type="password" name="login_password" id="login_password"/><br>

<input type="submit" id="login_submit" value="Login" /><br>
<input type="reset" id="login_reset" value="Reset"><br>
</form>

 <textarea id="message" style="color: white; 
 background-color: grey; visibility: hidden"  readonly> Default </textarea>

</div>

这是我的 jquery 移动代码:

$(document).ready(function(){ 

$('#login_submit').click(function(){

     $('message').change(function(success) {

     //How do I receive a variable from the html code?
     //success should be random either true or false
     //success variable is a callback from the server, but is not implemented yet


      if(success == true) {

      //I don´t know what to write in here, it should fade in the text area, 
      //change the background to green with white font and 
      // display the message  "Successful login!"
      } 

      if(success == false) {
      //error handler, 
      //change the background to green with white font and 
      // display the error message (if possible), otherwise "Unsuccessful login!"
      }

 });

});

有人能告诉我要为代码添加什么吗?

非常感谢!

【问题讨论】:

    标签: javascript html jquery-mobile mobile textarea


    【解决方案1】:

    这是我想出的:http://jsfiddle.net/tymeJV/YL6Da/

    我在代码中删除了您的样式属性,并根据登录失败或成功添加了类。

    jQuery

    if (success) {
         $("#message").fadeIn("slow").removeAttr("style").addClass("success").text("Successful!");
    } else {
         $("#message").fadeIn("slow").removeAttr("style").addClass("error").text("Failed");
    }
    

    CSS

    .success {
        background-color: green;
        color: white;
    }
    
    .error {
        background-color: red;
        color: white;
    }
    

    【讨论】:

    • 谢谢你,这对我帮助很大!我刚刚从
    • if(success) { $("#message").fadeIn("slow").addClass("success").text("Login successful!"); } else { $("#message").fadeIna("slow").addClass("error").text("Login not successful!"); } });
    【解决方案2】:

    您可以使用 jQuery .css() 来修改元素的 CSS 属性,如下所述:http://api.jquery.com/css/

    【讨论】:

      【解决方案3】:
      if(success == false) {
            $('#message').css("background-color","red");
            $('#message').css("color","white");
            $('#message').text('whatever text you want');
            $('#message').show();
      }
      else {
           $('#message').hide();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多