[英]Code not running properly due to javascript getting PHP values and i don't know how to compare them
<div class="content">
<h1>Üdvözöllek, <?php echo $nickname; ?>! </h2>
<h4>Válasszon hetet: </h4>
<select class="select-selected" id="1" name="het" onchange="checkclass()">
<option disabled selected value>Hét</option>
<option value="1"><?php echo $sd->format('m.d')," - ", $ed->format('m.d');?></option>
<option value="2"><?php echo $sd2->format('m.d')," - ", $ed2->format('m.d');?></option>
<option value="3"><?php echo $sd3->format('m.d')," - ", $ed3->format('m.d');?></option>
</select>
</div >
<div>
<a id="class">
<select class="select-selected" id="2" onchange="checksubject()" name="osztaly">
<option disabled selected value><?php echo $class; ?></option></select>
</a>
<a id="subject">
<select class="select-selected" id="3" name="tantargy">
<option disabled selected value><?php echo $subject; ?></option></select>
</a>
<a id="bontotte"></a>
<a id="classtwo"></a>
</div>
<script>
function checkclass() {
var het = document.getElementById('1').value;
if(1<=het){
var onosztaly =
'<select class="select-selected" name="osztaly">'+
'<option disabled selected value><?php echo $class; ?>'+
'<option value="<?php echo $class1; ?>"><?php echo $class1; ?></option>'+
'<option value="<?php echo $class2; ?>"><?php echo $class2; ?></option>'+
'<option value="<?php echo $class3; ?>"><?php echo $class3; ?></option>'+
'<option value="<?php echo $class4; ?>"><?php echo $class4; ?></option>'+
'<option value="<?php echo $class5; ?>"><?php echo $class5; ?></option>'+
'<option value="<?php echo $class6; ?>"><?php echo $class6; ?></option>'+
'<option value="<?php echo $class7; ?>"><?php echo $class7; ?></option>'+
'</select>';
document.getElementById("class").innerHTML =(onosztaly);
}
}
function checksubject() {
var sub = document.getElementById('2').value;
var clas = document.getElementById('2').value;
if(sub == clas){
var onsub = '<select class="select-selected" id="2" name="tantargy">'+
'<option disabled selected value><?php echo $subject; ?></option>'+
'<option value="<?php echo $subject1; ?>"><?php echo $subject1; ?></option>'+
'<option value="<?php echo $subject2; ?>"><?php echo $subject2; ?></option>'+
'<option value="<?php echo $subject3; ?>"><?php echo $subject3; ?></option>'+
'<option value="<?php echo $subject4; ?>"><?php echo $subject4; ?></option>'+
'</select>';
document.getElementById("subject").innerHTML =(onsub);
}
}
</script>
So the last one doesn't make it's select box to fill in, like the other one named "osztaly".所以最后一个不会让它成为选择框来填充,就像另一个名为“osztaly”的框。 I think i have a problem with the IF tag in the javascript because the values can't compare :)
我想我在 javascript 中的 IF 标签有问题,因为这些值无法比较:)
You have overwritten your own select box via javascript您已经通过 javascript 覆盖了自己的选择框
Change this code更改此代码
var onosztaly =
'<select class="select-selected" name="osztaly">'+
To到
var onosztaly =
'<select class="select-selected" id="2" onchange="checksubject()" name="osztaly">'+
it will work, means both the ID of the select box and the change event is lost when date is changed它将起作用,这意味着更改日期时,选择框的 ID 和更改事件都会丢失
to be frank your design is not good, there are effective methods to add options and remove options, innerHTML is very old style坦白说你的设计不是很好,有有效的方法来添加选项和删除选项,innerHTML是很老的风格
the question text shows -- javascript getting PHP values
-- this understanding is wrong问题文本显示
javascript getting PHP values
——这种理解是错误的
your entire code rendered as running HTML and given to browser, there is no more PHP in browser, only browser coding you are doing for drop down change您的整个代码呈现为运行 HTML 并提供给浏览器,浏览器中不再有 PHP,只有您正在为下拉更改所做的浏览器编码
so when you think that are actually reading values from PHP, that is not a correct understanding所以当你认为实际上是从 PHP 读取值时,这不是一个正确的理解
you are directly reading HTML components value only in javascript您仅在 javascript 中直接读取 HTML 组件值
As Dickens got the right way to make the code work, here is an example of correct output.由于狄更斯找到了使代码工作的正确方法,以下是正确输出的示例。 Essentially source has some minor errors, which were eliminated here
本质上 source 有一些小错误,这里已经消除了
function checkclass() { var het = document.getElementById('1').value; if(1<=het){ var onosztaly = '<select class="select-selected" id="2" onchange="checksubject()" name="osztaly">'+ '<option disabled selected value>class</option>'+ '<option value="class1">class1</option>'+ '<option value="class2">class2</option>'+ '<option value="class3">class3</option>'+ '<option value="class4">class4</option>'+ '<option value="class5">class5</option>'+ '<option value="class6">class6</option>'+ '<option value="class7">class7</option>'+ '</select>'; document.getElementById("class").innerHTML =(onosztaly); } } function checksubject() { var onsub = '<select class="select-selected" name="tantargy">'+ '<option disabled selected value>subject</option>'+ '<option value="subject1">subject1</option>'+ '<option value="subject2">subject2</option>'+ '<option value="subject3">subject3</option>'+ '<option value="subject4">subject4</option>'+ '</select>'; document.getElementById("subject").innerHTML =(onsub); }
<div class="content"> <h1>Üdvözöllek, nickname! </h2> <h4>Válasszon hetet: </h4> <select class="select-selected" id="1" name="het" onchange="checkclass()"> <option disabled selected value>Hét</option> <option value="1">date1</option> <option value="2">Date2</option> <option value="3">Date3</option> </select> </div > <div> <a id="class"> <select class="select-selected" id="2" onchange="checksubject()" name="osztaly"> <option disabled selected value>Class</option></select> </a> <a id="subject"> <select class="select-selected" id="3" name="tantargy"> <option disabled selected value>Subject</option></select> </a> <a id="bontotte"></a> <a id="classtwo"></a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.