0

我有一个 id='red' 的按钮。当我点击它时,我希望那些空的文本区域变成红色。我的代码不起作用。

$(document)ready.(function() {

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

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

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

});

非常感谢您的意见。我还是不给你工作。也许是我的标记搞砸了。

我在这里尝试了每一种方法,但如果不起作用,该死的。

哦,我还不允许发布图片=(。我解释一下。

在我的网站上,我有 5 个文本字段,当我按下注册按钮时,如果它们为空,我想要一个红色的 bg。但前提是它们是空的。

4

7 回答 7

3

你可以用这个,

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

    if($(this).val() == '') 
    {
        $(this).css('background-color' , '#FF0000');
    }
});
});
于 2013-07-16T07:04:15.740 回答
1

您可以选择文本区域,然后根据值为空的值过滤它们:

$('#reg').click(function() {
    $('textarea').filter(function(){
        return $(this).val() == '';
    }).css('background-color', 'red');
});
于 2013-07-16T06:50:24.937 回答
1

尝试使用.text()background-color改为color喜欢

if($('input[type="text"]').text() == '' && empty($('input[type="text"]').text())) {
    $(this).css({'background-color':'red'});
    or
    $(this).css('background-color','red');
}
于 2013-07-16T06:48:41.897 回答
0
<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');
    });
});
于 2013-07-16T07:28:33.623 回答
0

希望这会有所帮助,试试这个-

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');
     }
  });    
});

看演示

于 2013-07-16T07:18:31.833 回答
-1

试试这个代码:

$(function() {
    $('#reg').click(function() {
        if($(this).text() == '') {
            $(this).css({'color':'red'});
        }
    });
});
于 2013-07-16T06:50:01.830 回答
-1

你在第 4 行做错了。更改为这一行:

$(this).css('color','red');
于 2013-07-16T06:50:44.883 回答