简体   繁体   English

如何使用jQuery在鼠标悬停时更改按钮背景颜色?

[英]How to change button background color onmouseover with jquery?

I'm trying to make change the background color of the submit button if the fields aren't filled in. 如果没有填写字段,我试图更改“提交”按钮的背景颜色。

For example: If I didn't fill in all the inputs and textareas then onmousehover the background of the submit button should change to red. 例如:如果我没有填写所有输入和文本区域,则鼠标悬停后,提交按钮的背景应变为红色。 But if all of the fields are filled in, then it should change to the color green (onmousehover as well). 但是,如果所有字段均已填写,则应更改为绿色(鼠标悬停也应为绿色)。

I already have the code for the filled inputs so I just need the function to make the color changes: 我已经有了用于填充输入的代码,因此我只需要用于更改颜色的函数即可:

        (function() {
      $('form > input, form > textarea').keyup(function() {

        var empty = false;
        $('form > input, form > textarea').each(function() {
          if ($(this).val() == '') {
            empty = true;
          }
        });

        if (empty) {
          $('#active').attr('disabled', 'disabled');

        } else {
          $('#active').removeAttr('disabled');

        }
      });
    })()

My Html: 我的HTML:

                <form method="post" action="send.php" >
                <input name="name" type="text" class="topinp placeholdprop" style="float: left;" placeholder="Your cool name" />
                <input name="email" type="text" class="topinp placeholdprop" style="float: right;" placeholder="Your awesome email" />
                <input name="subject" type="text" class="subinp placeholdprop" placeholder="What do you need help with?" />
                <textarea name="comment" class="placeholdprop textareacntr" rows="7" type="text" placeholder="Type your message here" ></textarea>
                <input id="active" type="submit" value="Submit" disabled="disabled" />
            </form>

The functions should go after if (empty) I believe. 我认为这些功能应该遵循if (empty) Please help. 请帮忙。

Well in your title you said you wanted to change a div, but seems like you want to change the color of the button. 在标题中,您曾说过要更改div,但似乎要更改按钮的颜色。 I am not sure how to change the color of a default submit button unless you change it with lets say Jquery UI. 我不确定如何更改默认提交按钮的颜色,除非您使用Jquery UI进行更改。 But let me show you the code for hover over for the div: 但是,让我向您展示将鼠标悬停在div上的代码:

var original_color = '';
$('#divID').hover(function(){
    //hover code comes here
    original_color = $(this).css('background-color');
    divColor = function you use to check wether or not fields are empty;
    $(this).css('background-color', divColor);
}, function(){
    //mouse leave code comes here
    $(this).css('background-color', original_color);
});

that may be the interesting part of the solution: 这可能是解决方案中有趣的部分:

  $('#active').hover(function() {
    $(this).css("background-color", empty?"red":"green")
  });

the rest can be found in the snippet: 其余的可以在代码段中找到:

  (function() { $('form > input, form > textarea').keyup(function() { var valid = true; $('form > input, form > textarea').each(function() { if ($(this).val() == '') { valid = false; } }); $('#active').prop('disabled', !valid); $('#active').toggleClass ('valid', valid); }); })() 
 #active:hover { background-color: red; } #active.valid:hover { background-color: green; } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" action="send.php" > <input name="name" type="text" class="topinp placeholdprop" placeholder="Your cool name" /> <input name="email" type="text" class="topinp placeholdprop" placeholder="Your awesome email" /> <input name="subject" type="text" class="subinp placeholdprop" placeholder="What do you need help with?" /> <textarea name="comment" class="placeholdprop textareacntr" rows="7" type="text" placeholder="Type your message here" ></textarea> <input id="active" type="submit" value="Submit" disabled="disabled"/> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM