我希望湿度和人数字段具有与我的温度字段相同的功能

时间:2017-07-16 09:54:09

标签: javascript php jquery onclick

我有三个字段。在我的温度字段中,onclick on按钮应该变色以及在会话变量中存储值。我做到了

同样地,我希望我的humdidty和人数字段具有以下功能:点击第一个按钮,它应该变色并在会话中存储'1'作为值。点击第二个按钮,我想要第一个和第二个按钮变色并将会话变量中的值存储为“2”。点击第3个按钮,我想要第1个和第2个和第3个按钮变色并将值存储为“3”。点击第4个按钮,我希望所有按钮都被着色并将值存储为“4”。

我是jQuery的新手,我正在努力做到这一点。我怎么能这样做?

<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("chk.php", { 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;
}
.button1{
  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;
}
.button2{
  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>

<div align="left">Temperature </div>
<form action='chk.php' method='post'>
  <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">
<br><br>
<div align="left">Humidity</div>
  <input type="button" class="button1" value="1">
  <input type="button" class="button1" value="2">
  <input type="button" class="button1" value="3">
  <input type="button" class="button1" value="4">
<br><br>
<div align="left">Number of people </div>
  <input type="button" class="button2" value="1">
  <input type="button" class="button2" value="2">
  <input type="button" class="button2" value="3">
  <input type="button" class="button2" value="4">
<br><br>
<input type='submit' value='submit'>
<input type='reset' value='reset'>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

你或许可以像这样处理问题 - 会话处理只是一个粗略的例子,但我认为这就是我理解的问题。

<?php
    session_start();

    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( !empty( $_POST['bttn'] ) && !empty( $_POST['type'] ) ){

            $type=$_POST['type'];
            $bttn=$_POST['bttn'];


            $_SESSION['buttonClicked'][ $type ]=$bttn;

            exit( json_encode( $_SESSION['buttonClicked'] ) );
        }
    }
?>

<!doctype html>
<html>
    <head>
        <meta charset='utf-8' />
        <title>Set Colours of Buttons</title>
        <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: black;
            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;
        }
        input[type='button']{
            border: 1px solid black;
            padding: 8px 30px;
            margin:0 0.25rem;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            cursor: pointer;
            float: left;
        }
        </style>
        <script>
            (function(){
                var colours={
                    1:'red',
                    2:'blue',
                    3:'green',
                    4:'yellow'
                };
                var flags={ 
                    passive:true,
                    capture:false
                };
                function setcolours(e){
                    var _class=this.dataset.class;
                    var col=this.parentNode.querySelectorAll('input[type="button"][data-class="'+_class+'"]');

                    /* Clear previous colour classes assigned */
                    col.forEach(function(e,i,a){
                        Object.values( colours ).forEach(function( c ){
                            e.classList.remove( c );
                        });
                    });

                    /* Add colour class to any element with a value equal to or less that selected button value */
                    for( var i=this.value; i > 0; i-- ){
                        try{
                            if( col[ i - 1 ].nodeType==1 )col[ i - 1 ].classList.add( colours[ col[ i - 1 ].value ] )
                        }catch( err ){
                            console.info( err );
                            continue;
                        }
                    }
                    ajax( this.value, this.dataset.type );
                }
                function ajax( value, type ){
                    var xhr=new XMLHttpRequest();
                    xhr.onreadystatechange=function(){
                        if( xhr.readyState==4 && xhr.status==200 ){
                            document.getElementById('results').innerHTML=this.response;
                        }
                    };
                    var params='bttn='+value+'&type='+type;
                    xhr.open( 'post', location.href, true );
                    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    xhr.send( params );
                }
                function bindEvents(e){
                    var col = document.querySelectorAll('input[type="button"]');
                    if( col && col.length > 0 ){
                        for( var n in col ){
                            if( col[ n ].nodeType==1 ){
                                col[ n ].addEventListener( 'click', setcolours.bind( col[ n ] ), flags );
                            }
                        }
                    }
                }
                document.addEventListener( 'DOMContentLoaded', bindEvents, flags );
            }());
        </script>
    </head>
    <body>

        <form action='chk.php' method='post'>
            <div align="left">Temperature </div>

            <input type="button" class="button" data-class='b' data-type='temperature' value="1">
            <input type="button" class="button" data-class='b' data-type='temperature' value="2">
            <input type="button" class="button" data-class='b' data-type='temperature' value="3">
            <input type="button" class="button" data-class='b' data-type='temperature' value="4">
            <br />
            <br />
            <div align="left">Humidity</div>
            <input type="button" class="button1" data-class='b1' data-type='humidity' value="1">
            <input type="button" class="button1" data-class='b1' data-type='humidity' value="2">
            <input type="button" class="button1" data-class='b1' data-type='humidity' value="3">
            <input type="button" class="button1" data-class='b1' data-type='humidity' value="4">
            <br />
            <br />
            <div align="left">Number of people </div>
            <input type="button" class="button2" data-class='b2' data-type='people' value="1">
            <input type="button" class="button2" data-class='b2' data-type='people' value="2">
            <input type="button" class="button2" data-class='b2' data-type='people' value="3">
            <input type="button" class="button2" data-class='b2' data-type='people' value="4">
            <br />
            <br />
            <input type='submit' value='submit'>
            <input type='reset' value='reset'>
        </form>

        <pre id='results'></pre>
    </body>
</html>

由于天气恶劣,我可以在笔记本电脑上花一点时间,最后修改了标记,css&amp;的JavaScript。

<?php
    session_start();

    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( !empty( $_POST['bttn'] ) && !empty( $_POST['type'] ) ){

            $type=$_POST['type'];
            $bttn=$_POST['bttn'];


            $_SESSION[ 'buttonClicked' ][ $type ]=$bttn;

            header( 'HTTP/1.1 200 OK', true, 200 );
            header( 'Content-Type: application/json' );
            exit( json_encode( $_SESSION[ 'buttonClicked' ] ) );
        }
    }
?>

<!doctype html>
<html>
    <head>
        <meta charset='utf-8' />
        <title>Set Colours of Buttons</title>
        <style>
            input[type='button']{
                background-color:white;
                border: 1px solid black;
                padding: 0.5rem 2rem;
                margin:0 0.25rem;
                text-align: center;
                text-decoration: none;
                display: inline-block;
                cursor: pointer;
                float: left;
            }
            .green{
                background-color: green!important;
                color: white;
            }
            .blue{
                background-color: blue!important;
                color: white;
            }
            .yellow{
                background-color: yellow!important;
                color: black;
            }
            .red{
                background-color: red!important;
                color: white;
            }
            .pink{
                background-color: pink!important;
                color: black;
            }
            .orange{
                background-color: orange!important;
                color: white;
            }
            .purple{
                background-color: purple!important;
                color: white;
            }
            .brown{
                background-color: brown!important;
                color: white;
            }
            legend,fieldset{
                border:none;
            }
            legend{
                border-bottom:1px solid gray;
                padding:0.5rem;
            }
        </style>
        <script>
            (function(){
                var colours={
                    1:'red',
                    2:'orange',
                    3:'yellow',
                    4:'pink',
                    5:'brown',
                    6:'purple',
                    7:'blue',
                    8:'green'
                };
                var flags={ 
                    passive:true,
                    capture:false
                };
                function setcolours(e){
                    var _type=this.parentNode.dataset.type;
                    var col=this.parentNode.querySelectorAll( 'input[type="button"]' );


                    /* Clear previous colour classes assigned */
                    col.forEach(function(e,i,a){
                        Object.values( colours ).forEach(function( c ){
                            e.classList.remove( c );
                        });
                    });

                    /* Add colour class to any element with a value equal to or less that selected button value */
                    for( var i=this.value; i > 0; i-- ){
                        try{
                            if( col[ i - 1 ].nodeType==1 )col[ i - 1 ].classList.add( colours[ col[ i - 1 ].value ] )
                        }catch( err ){
                            console.info( err );
                            continue;
                        }
                    }

                    /* send the ajax request to store values into session variables &/or whatever actions are required */
                    ajax( this.value, this.parentNode.dataset.type );
                }

                function ajax( value, type ){
                    var xhr=new XMLHttpRequest();
                    xhr.onreadystatechange=function(){
                        if( xhr.readyState==4 && xhr.status==200 ){
                            document.getElementById('results').innerHTML=this.response;
                        }
                    };
                    var params='bttn='+value+'&type='+type;
                    xhr.open( 'post', location.href, true );
                    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    xhr.send( params );
                }
                function bindEvents(e){
                    var col = document.querySelectorAll('form > fieldset > input[type="button"]');
                    if( col && col.length > 0 ){
                        for( var n in col ){
                            if( col[ n ].nodeType==1 ){
                                col[ n ].addEventListener( 'click', setcolours.bind( col[ n ] ), flags );
                            }
                        }
                    }
                }
                document.addEventListener( 'DOMContentLoaded', bindEvents, flags );
            }());
        </script>
    </head>
    <body>

        <form action='chk.php' method='post'>

            <fieldset data-type='temperature'>
                <legend>Temperature</legend>
                <input type="button" value="1" />
                <input type="button" value="2" />
                <input type="button" value="3" />
                <input type="button" value="4" />
                <input type="button" value="5" />
                <input type="button" value="6" />
                <input type="button" value="7" />
                <input type="button" value="8" />
            </fieldset>

            <fieldset data-type='humidity'>
                <legend>Humidity</legend>
                <input type="button" value="1" />
                <input type="button" value="2" />
                <input type="button" value="3" />
                <input type="button" value="4" />
                <input type="button" value="5" />
                <input type="button" value="6" />
                <input type="button" value="7" />
                <input type="button" value="8" />
            </fieldset>

            <fieldset data-type='people'>
                <legend>Number of people</legend>
                <input type="button" value="1" />
                <input type="button" value="2" />
                <input type="button" value="3" />
                <input type="button" value="4" />
                <input type="button" value="5" />
                <input type="button" value="6" />
                <input type="button" value="7" />
                <input type="button" value="8" />
            </fieldset>

            <br />
            <br />

            <input type='submit' value='submit'>
            <input type='reset' value='reset'>
        </form>

        <pre id='results'></pre>
    </body>
</html>