简体   繁体   English

切换HTML中优先级排序表的组合框的值? 如何修复我的代码?

[英]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>&uarr;</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>
&uarr;
</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! 我真的不知道我的代码有什么问题... 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; 我不确定这是否可以解决您的问题,因为我尚未测试过,但是我注意到您的第一个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. 对于第一个for循环,break语句将在第一个循环上立即执行,从而使其始终在第一个迭代中退出循环。 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. 对于第二个和第三个循环,只有在满足if语句的条件并输入该控制块时,才执行break语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM