doujia2386 2017-07-15 17:30
浏览 88
已采纳

onclick on 3rd button,我希望第1,第2,第3个按钮变得有色。同样点击第4个按钮,我希望所有的按钮都变得有色。我怎么办? [关闭]

i have 4 buttons. onclick on 3rd button i want 1st,2nd,3rd button get coloured.Similarly onclick on 4th button,i want all the buttons get coloured.And even i want to save a value when button is clicked.like when first button is clicked, a value '1' is stored in session variable.i have to pass this variable to php file.and similarly onclick on second button,a value '2' is to be stored and same for 3rd n 4th button.how can i do this?? any kind of help will be useful

 
<html>
<head>
<script type='text/javascript'>
function change(that){
that.style.backgroundColor = 'yellow';
document.querySelector('.button').click()
}

</script>
<style>
.button {
  background-color: white;
  border: 1px solid black;
  color: white;
  padding: 8px 30px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  cursor: pointer;
  float: left;
}
</style>
<body>
  <input type="button" class="button" onclick="this.style.backgroundColor = 'red';" value="1">
  <input type="button" class="button" onclick="change(this)" value="2">
  <input type="button" class="button" onclick="this.style.backgroundColor = 'green';" value="3">
  <input type="button" class="button" onclick="this.style.backgroundColor = 'blue';" value="4">
</body>
</html>

</div>
  • 写回答

2条回答 默认 最新

  • dongzhiji0814 2017-07-15 19:19
    关注

    I do it with JQuery you need to modify the url to the php post to pass your variable value.

    regards,

     
       <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type='text/javascript'>
      $(document).ready(function(){
        var buttonClicked = "";
    
        $("input").on('click', function(){
    
          var thisDiv = $(this).val();
          buttonClicked = thisDiv;
          var classToAdd = "";
    
          $.post("yourUrlToPost", { buttonClicked: buttonClicked});
    
          console.log(thisDiv);
          switch(thisDiv){
            case "1": classToAdd = "red";
              break;   
            case "2":
              classToAdd = "blue";
              break;
            case "3":
              classToAdd = "green";
              break;
            case "4":
              classToAdd = "yellow";
              break;
            default:
              break;
          };
    
          $("input").each(function(index,value){
            var actualClass = $(value).attr("class");
            if(index < thisDiv){
              $(value).addClass(classToAdd).removeClass(actualClass);
            }else{
    
              if(actualClass != "button"){
                $(value).addClass("button").removeClass(actualClass);
              }
              
            }
    
            
            
          });
          
        });
      });
    </script>
    <?php
      $_SESSION["buttonClicked"] = $_POST["buttonClicked"];
    ?>
    <style>
    .green{
      background-color: green;
      border: 1px solid black;
      color: white;
      padding: 8px 30px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      cursor: pointer;
      float: left;
    }
    .blue{
      background-color: blue;
      border: 1px solid black;
      color: white;
      padding: 8px 30px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      cursor: pointer;
      float: left;
    }
    .yellow{
      background-color: yellow;
      border: 1px solid black;
      color: white;
      padding: 8px 30px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      cursor: pointer;
      float: left;
    }
    .red{
      background-color: red;
      border: 1px solid black;
      color: white;
      padding: 8px 30px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      cursor: pointer;
      float: left;
    }
    .button {
      background-color: white;
      border: 1px solid black;
      color: white;
      padding: 8px 30px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      cursor: pointer;
      float: left;
    }
    </style>
    <body>
      <input type="button" class="button" value="1">
      <input type="button" class="button" value="2">
      <input type="button" class="button" value="3">
      <input type="button" class="button" value="4">
    </body>
    </html>
    </html>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)