change background color on empty textarea

Deadly 提交于 2019-12-13 04:36:40

问题


I got a button with id='red'. When I click on it I want those textareas who are empty to turn red. My code is not working.

$(document)ready.(function() {

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

if($(':text').val() == '') {

$(this).css({'color':'red'});
            }
});

});

Thanks a lot for your input guys. I still not working thou. Maybe it is my markup who is screwed up.

I tried every sulotion here but if will not work, goddamn.

Oh I am no allowed to post image yet=(. I explain.

On my site I got 5 text fields that I want to have a red bg if they are empty when I press a register button. But only if they are empty.


回答1:


Try this code :

$(function() {
    $('#reg').click(function() {
        if($(this).text() == '') {
            $(this).css({'color':'red'});
        }
    });
});



回答2:


You can use this,

$( document ).ready(function() {
$('#reg').click(function() {

    if($(this).val() == '') 
    {
        $(this).css('background-color' , '#FF0000');
    }
});
});



回答3:


Try with .text() and put background-color instead color like

if($('input[type="text"]').text() == '' && empty($('input[type="text"]').text())) {
    $(this).css({'background-color':'red'});
    or
    $(this).css('background-color','red');
}



回答4:


You can select the textareas, then filter them based on the value being empty:

$('#reg').click(function() {
    $('textarea').filter(function(){
        return $(this).val() == '';
    }).css('background-color', 'red');
});



回答5:


<textarea id="myText"></textarea>
<input id="reg" type="button" value="register" name="btn_register">

$(document).ready(function () {
    $('#reg').click(function () {
        $("#myText").val() ? $("#myText").css('background-color', 'white') : $("#myText").css('background-color', 'red');
    });
});



回答6:


hope this will help,try this-

HTML-

<textarea col="2" style="width:200px;height:200px;"></textarea>
<input id="reg" type="button" value="submit">

js-

$(document).ready(function() {    
  $('#reg').click(function() {   
     if (!$("#myTextArea").val()) {    
        $("textarea").css('background-color','red');
     }
  });    
});

see demo




回答7:


u r doing wrong on line no 4. change to this line :

$(this).css('color','red');


来源:https://stackoverflow.com/questions/17669779/change-background-color-on-empty-textarea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!