Jquery无法设置隐藏字段值

Jquery not able to set hidden field values

本文关键字:字段 隐藏 设置 Jquery      更新时间:2023-09-26

我在尝试使用Jquery甚至使用纯java脚本设置隐藏字段时遇到了一个奇怪的问题

我的JSP页面中有两个隐藏字段

<input type="hidden" name="firstSelectedPaymentId" id="firstSelectedPaymentId1" value=""/>
<input type="hidden" name="secondSelectedCCPaymentId" id="secondSelectedCCPaymentId" value=""/>

这就是我在JS 中设置隐藏值的方式

if ($('#firstSelectedPaymentId1').length > 0) {
    $('#firstSelectedPaymentId1').val(response.result.selectedPaymentInfoId);
}
if ($('#secondSelectedCCPaymentId').length > 0) {
   //$('#secondSelectedCCPaymentId').val(response.result.selectedPaymentInfoId);            
  document.getElementById('secondSelectedCCPaymentId').value=response.result.selectedPaymentInfoId;
}

对于第一种情况,它运行良好,但对于secondSelectedCCPaymentId,它没有在隐藏字段中设置任何值。

我已经检查了我的JSP,并且没有具有相同id的字段,此外,如果我尝试

alert($('#secondSelectedCCPaymentId').val());

我可以在像5466565665666这样的警报框中看到值。response.result.selectedPaymentInfoId是系统生成的值,它是由我已经验证的系统生成的。我不确定我哪里做错了,或者为什么它没有在隐藏字段中设置值?

if ($('#firstSelectedPaymentId1').length > 0) {
    $('#firstSelectedPaymentId1').text(response.result.selectedPaymentInfoId);
}
if ($('#secondSelectedCCPaymentId').length > 0) {
   //$('#secondSelectedCCPaymentId').text(response.result.selectedPaymentInfoId);            
  document.getElementById('secondSelectedCCPaymentId').value=response.result.selectedPaymentInfoId;
}