奇怪的jQuery变量传递行为

AKo*_*Kor 1 forms jquery

我试图在点击按钮时将变量传递给隐藏的表单输入,如下所示:

        $('#convertToButton').click(function() {
            $("#currencySelectValue").val($("#currencySelect").val());
            $("#currencyValue").val($("#fromCurrencyValue").val());
            $.post("php/convertToForm.php", $("#convertToForm").serialize(), function(data){ 
                $('#toCurrencyValue').val(data);
            });
        });

        $('#convertFromButton').click(function() {
            $("#currencySelectValue").val($("#currencySelect").val());
            $("#btcValueForm").val($("#btcValue").val());
            $.post("php/convertFromForm.php", $("#convertFromForm").serialize(), function(data){ 
                $('#fromCurrencyValue').val(data);
            });
        });
Run Code Online (Sandbox Code Playgroud)

在第一个单击处理程序中,一切都很顺利.在第二个单击处理程序中,表单未显示已经过了#currencySelectValue.代码是一样的,所以我不确定问题出在哪里.

以下是表格:

<form action="php/convertToForm.php" id="convertToForm">
<input type="hidden" value="<?php echo $last; ?>" name="lastPrice" id="lastPrice"/>
<input type="hidden" name="currencySelectValue" id="currencySelectValue"/>
<input type="hidden" name="currencyValue" id="currencyValue"/>
<!-- get values of the currency selector, and the currency value -->
</form>
<form action="php/convertFromForm.php" id="convertFromForm">
<input type="hidden" value="<?php echo $last; ?>" name="lastPrice" id="lastPrice"/>
<input type="hidden" name="currencySelectValue" id="currencySelectValue"/>
<input type="hidden" name="btcValueForm" id="btcValueForm"/>
<!-- get values of the currency selector, and the BTC value -->
</form>
Run Code Online (Sandbox Code Playgroud)

除了#currencySelectValueconvertFromForm中的所有内容都可以正常传递.有帮助吗?

Dar*_*JDG 6

在整个文档中,永远不应该有多个具有相同ID的元素.ID必须是唯一的才能工作.

如果要选择副本或类似部分,请改用类.