简体   繁体   English

填充输入字段时更改按钮的颜色

[英]Change colour of button when input fields are filled

I open a div with a table inside by clicking a button. 我点击一个按钮打开一个带有桌子的div。 In this table there are a number of input fields. 在此表中有许多输入字段。 If all input fields are filled then button should change the colour. 如果所有输入字段都已填满,则按钮应更改颜色。

I have a number of divs with table with input fields. 我有一些带有输入字段表的div。 One of them is working, but does not succeed with seccond div. 其中一个正在工作,但没有成功使用seccond div。

<p><br></p> 

<!-- The Next Button Plates -->
<button id = "buttonP" onclick="showOrHide('Plates')" class="button1" name= "Plates" ><b>Plates</b></button> 
<!-- Insert a table in a div, so this can be hide -->
 <div id="Plates">
<br>    
<div id="CompoundPlates_button">
 <table style="width:20%;margin-left:50px;" >
 <colgroup>
    <col span="3" style="background-color:#E3CEF6;">
    <!--<col style="background-color:yellow"> -->
  </colgroup>
  <tr>
    <td width="20%"><input type="button" id = "button" class="buttonsmall" style="height:20px;width:60px" onclick="showOrHide('CompoundPlates')">      
    </td>
    <td width="40%"><b>CompoundPlates</></td>
    <td width="15%"></td> 
    <td width="15%"></td>
    <td width="10%"></td>   
  </tr> 
  </table>
 </div> <!-- Close Div CompoundPlates_button --> 
         <!-- Insert a table in a div, so this can be hide -->
 <div id="CompoundPlates">
   <table style="width:50%;margin-left:50px;" >
        <colgroup>
        <col span="3" style="background-color:#E3CEF6;">        
        </colgroup>
   <tr>
    <td  width="10%">      
    </td>
    <td  width="20%">Number of Plates:</td>
    <td  width="30%"><input type="text" name="Number of plates" size="35"></td> 
    <td  width="20%"></td>
    <td  width="20%"></td>  
  </tr> 
  <tr>
    <td>      
    </td>
    <td>Plate/Tip type :</td>
    <td><input type="text" name="Plate/Tip type" size="35"></td> 
    <td></td>
    <td></td>   
  </tr> 
  <tr>
    <td>
    </td>
    <td>Lid : </td>
    <td><input type="text" name="Lid" size="35"> </td>
    <td></td>
    <td></td>
   </tr>
   <tr>
    <td>
    </td>
    <td>Storage device :</td>
    <td><input type="text" name="Storage device" size="35"></td> 
    <td></td>
    <td></td>   
  </tr> 
  <tr>
    <td>
    </td>
    <td>Position :</td>
    <td><input type="text" name="Position" size="35"></td>
    <td></td>
    <td></td>    
  </tr> 
 </table>
 </div> <!-- Close div CompoundPlates -->
</div> <!-- Close div Plates -->

If input boxes are filled then color of button Compound plates should change from red to green. 如果填充了输入框,则按钮复合板的颜色应从红色变为绿色。 And if button Compound plates and button Assay plates is green then overlaying button Plates should also change from red to green 如果按钮复合板和按钮分析板为绿色,则覆盖按钮板也应从红色变为绿色

I think it's helpful for you 我觉得它对你有帮助

[1]: Change the button's color after fill out the form [1]: 填写表格后更改按钮的颜色

$("input[type=text]").change(function() {
  var filled = true;
  $( "input[type=text]" ).each(function( index ) {
  if($( this ).val() == ""){
    filled = false;
  });
  if(filled === false){
    //change color
  }
});

Should do the trick. 应该做的伎俩。

What I did is I used Jquery to check and change the color of the button and for the input fields I used css because it was hard for me to change more than one input field. 我做的是我使用Jquery来检查和更改按钮的颜色以及我使用css的输入字段,因为我很难更改多个输入字段。 This is my fiddle 这是我的小提琴

 $("input[type='text'], textarea").on("input", function () { canChangeColor(); }); function canChangeColor(){ var can = true; $("input[type='text'], textarea").each(function(){ if($(this).val()==''){ can = false; } }); if(can){ $('.btn').css({background:'green'}) }else{ $('.btn').css({background:'red'}) } } 
 .form-control { background-color: red; } .form-control:valid { background-color: green; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div> <input type="text" class="form-control" id="name" name="user_name" placeholder=" Full Name" required> </div> <div> <input type="text" class="form-control" name="user_mail" placeholder=" Email Address" required> </div> <div> <textarea type="text" class="form-control" name="user_message" id="message" cols="30" rows="10" placeholder=" Let me know what's on your mind" required></textarea> </div> </div> <div class="sendButton"> <button class="btn" type="submit" disabled="disabled">Send</button> </div> 

Hope this works for you! 希望这对你有用!

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

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