提交带有已使用jQuery移动的列表框项目的隐藏字段

Submit hidden fields with listbox items that have been moved using jQuery

本文关键字:列表 项目 字段 隐藏 移动 jQuery 提交      更新时间:2023-09-26

我有两个列表框,可以使用jQuery在其中移动项目。代码摘要如下:

<select multiple size="10" id="from">...</select>
<select multiple id="to" size="10" name="subitted array[]">...</select>

一些按钮,当点击时从上面的列表框中移动项目:

<a href="javascript:moveSelected('from', 'to')">&gt;</a>...
<a href="javascript:moveSelected('to', 'from')">&lt;</a> 
<a href="javascript:moveAll('to', 'from')" href="#">&lt;&lt;</a>

jQuery函数:

function moveAll(from, to) {
    $('#'+from+' option').remove().appendTo('#'+to); 
}
function moveSelected(from, to) {
    $('#'+from+' option:selected').remove().appendTo('#'+to); 
}

我还有以下功能,我呼吁提交:

<form name="selection" method="post" onSubmit="return selectAll()">... </form>
function selectAll() {
    $("select option").attr("selected","selected");
}

但这只会通过POST返回右侧列表框中的值。我还想提交表单中的其他字段(例如隐藏字段)。我该怎么做?

非常感谢。

如果你的下拉列表在你的表单中,你不需要任何特殊处理,只需提交你的表单和表单中的所有字段,就会发送到你的控制器。

在JS中:

$('#formId').submit();

在您的html:中

<button type="submit">Send</button>

这两个选项中的任何一个都适合你的问题,如果我能理解你的问题的话,请告诉我。

此外,您应该删除此代码"onSubmit="return selectAll()"并使表单提交工作正常,将所有数据发送到表单