在 JavaScript 中更改多个值时遇到问题

Trouble changing multiple values in javascript

本文关键字:遇到 问题 JavaScript      更新时间:2023-09-26

好吧,我遇到了一个问题。我正在修改 ATS 系统,需要添加一些功能,但我在 JavaScript 中遇到了一件奇怪的事情。基本上,我需要根据下拉菜单中的选择更改一组表单值,并且代码有效...当只有一件事要改变时。当我添加需要更改它的其他部分时,它停止工作。我已经跑遍了互联网和许多 Stack 问题,但似乎没有任何帮助。有效的代码是:

<form id="test" name="test">
        <select id="statusID" name="statusID" onchange="javascript: return updateForm();">
                <option value="" selected></option>
                <option value="test1">One</option>
                <option value="2">Two</option>
        </select>
        <br />
        <input type="hidden" value="Test" id="RS" name="RS" />
    </form>
<script type='text/javascript'>
function updateForm()
{   
    if (document.getElementById('statusID').selectedIndex  == "2") 
                document.getElementById('RS').value = '200' 
                else if(document.getElementById('statusID').selectedIndex == "1")
                document.getElementById('RS').value = 'RSP'
}
</script>

当我在服务器上测试它时,这有效,但当我添加需要填充的其他字段时,它完全停止工作。我正在使用的JavaScript如下。我已经根据我看到的代码尝试了一些变体,但无济于事。

<script type='text/javascript'>
function updateForm()
{
    if (document.getElementById('statusID').selectedIndex  == "2") 
                document.getElementById('RS').value = '200'
                document.getElementById('RSDATE').value = '200'
                document.getElementById('RSP').value = '200'
                else if(document.getElementById('statusID').selectedIndex == "1")
                document.getElementById('RS').value = 'RSP'
                document.getElementById('RSDATE').value = 'RSP'
                document.getElementById('RSP').value = 'RSP'          
            }
</script>

应该指出,我根本不精通JavaScript。我也不精通PHP...但我正在修改基于 PHP 的 ATS 系统。这段代码是我和目标(完成ATS系统)之间的倒数第二个块,因此将不胜感激。

从问题的上下文来看,您应该在 if 和 else if 后面添加大括号:(战斗也注意到,应添加分号)

        if (document.getElementById('statusID').selectedIndex  == "2")  {
            document.getElementById('RS').value = '200';
            document.getElementById('RSDATE').value = '200';
            document.getElementById('RSP').value = '200';
        }
        else if(document.getElementById('statusID').selectedIndex == "1") {
            document.getElementById('RS').value = 'RSP';
            document.getElementById('RSDATE').value = 'RSP';
            document.getElementById('RSP').value = 'RSP';      
        }

您需要在每行末尾使用分号,并且每个条件都需要大括号。

function updateForm() {
    if (document.getElementById('statusID').selectedIndex == "2") {
        document.getElementById('RS').value = '200';
        document.getElementById('RSDATE').value = '200';
        document.getElementById('RSP').value = '200';
    }
    else if (document.getElementById('statusID').selectedIndex == "1") {
        document.getElementById('RS').value = 'RSP';
        document.getElementById('RSDATE').value = 'RSP';
        document.getElementById('RSP').value = 'RSP';
    }
}​

当您要执行操作块时,条件后需要跟括号。例如:

if (document.getElementById('statusID').selectedIndex  == "2") {
            document.getElementById('RS').value = '200'
            document.getElementById('RSDATE').value = '200'
            document.getElementById('RSP').value = '200'
}