gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:27:07 25 4
gpt4 key购买 nike

我有三个字段。在我的温度字段中,单击按钮应该变色,并在 session 变量中存储一个值。我就是这么做的。

类似地,我希望我的湿度和人数字段具有如下功能:单击第一个按钮时,它应该着色并将“1”作为值存储在 session 变量中。单击第二个按钮时,我希望第一个和第二个按钮着色并将 session 变量中的值存储为“2”。单击第三个按钮时,我希望第一个、第二个和第三个按钮着色并将值存储为“3”。单击第四个按钮时,我希望所有按钮都着色并将值存储为“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>

最佳答案

您也许可以这样解决问题 - session 处理只是一个粗略的示例,但我认为这符合我对问题的理解。

<?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 和 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>

关于javascript - 我希望湿度和人数字段与我的温度字段具有相同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45127236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com