Switch values of comboboxes for priority sorting table in HTML? How do I fix my code?
I would like to make a table wherein the headers have priorities placed in comboboxes. I want it so that when I change the priority of a combobox, the other combobox that holds that number will switch with the changed one, then refresh the page to be re-sorted. I tried to make such code:
<script>
function switchpriority(priorityid){
var t = document.getElementById("typepriority");
var n = document.getElementById("numpriority");
var d = document.getElementById("datepriority");
if(priorityid=="typepriority"){
for(i = 1; i < 4; i++){
if(n.value!=i && d.value!=i){
if(t.value==n.value){
n.value = i;
}else 开发者_开发问答if(t.value==d.value){
d.value = i;
}
}
break;
}
}else if(priorityid=="numpriority"){
for(i = 1; i < 4; i++){
if(t.value!=i && d.value!=i){
if(n.value==t.value){
t.value = i;
}else if(n.value==d.value){
d.value = i;
}
break;
}
}
}else if(priorityid=="datepriority"){
for(i = 1; i < 4; i++){
if(t.value!=i && n.value!=i){
if(d.value==t.value){
t.value = i;
}else if(d.value==n.value){
n.value = i;
}
break;
}
}
}
self.location = 'searchorder.php?typepriority='+t.value+'&numpriority='+n.value+'&datepriority='+d.value;
}
</script>
<table>
<tr>
<td>
Type
<select name="typepriority" id="typepriority" onchange="switchpriority('typepriority')">
<?php
for($i=1;$i<4;$i++){
if(isset($_GET['typepriority'])){
if($_GET['typepriority']==$i){
echo '<option selected="selected">'.$i.'</option>';
continue;
}
}
echo '<option>'.$i.'</option>';
}
?>
</select>
<label>↑</label>
</td>
<td>
No.
<select name="numpriority" id="numpriority" onchange="switchpriority('numpriority')">
<?php
for($i=1;$i<4;$i++){
if(isset($_GET['numpriority'])){
if($_GET['numpriority']==$i){
echo '<option selected="selected">'.$i.'</option>';
continue;
}
}
echo '<option>'.$i.'</option>';
}
?>
</select>
↑
</td>
<td>
Date
<select name="datepriority" id="datepriority" onchange="switchpriority('datepriority')">
<?php
for($i=1;$i<4;$i++){
if(isset($_GET['datepriority'])){
if($_GET['datepriority']==$i){
echo '<option selected="selected">'.$i.'</option>';
continue;
}
}
echo '<option>'.$i.'</option>';
}
?>
</select>
</td>
</tr>
</table>
It works so far, but somehow, when I change the value of a combobox consecutively, it doesn't work. I really don't know what's wrong with my code... tch! Or is there a better way to do this?
I'm not sure if this will fix your problem because I haven't tested it, but I notice that your first break;
statement is in a different spot than the other two:
// 1st for loop
for (i = 1; i < 4; i++) {
if (...) {
// ...
}
break;
}
// 2nd and 3rd for loops
for (i = 1; i < 4; i++) {
if (...) {
// ...
break;
}
}
In the case of the first for
loop, the break statement will execute immediately on the first loop, causing it to exit the loop on the first iteration, always. In the case of the second and third loops, you only execute the break statement when you satisfy the condition of the if
statement and enter that control block.
精彩评论