0

我有一个表格,我正在填写一个 ajax 请求。大多数情况都很好,但我似乎无法将下拉列表的值分配给隐藏变量。

想法是这样的:您选择一个先前完成的作业,提交一个 ajax 请求,然后我从该请求中填写表单的所有值。

您还可以选择移除(删除)此分配。为此,我需要获取 id 的值(它在 ajax 请求中返回)并将其填充到隐藏字段中。这个隐藏的字段是不同的形式。当我尝试提交此表单时,该字段未填写。

示例代码:

function loadQuery() {
    var assign = $("#existingAssignment").val(); // Get the value of the select box so we can build the next page
    $.ajax({
        type: 'get',
        url: 'http://127.0.0.1/WMT/model/getCandidate.cfc',
        data: {
            method: 'getExistingAssignPosInfo',
            tourid: assign
        },
        dataType: 'json',
        async: false,
        success: function (result) {

            $('#removeAssignment').show(); // This is the name of the hidden field. We Show this when we get the JSON
            $('#removeAssignmentID').val(result.DATA[0][0]);

        }
    });
}

HTML

<form class="form-horizontal" action="">
    <div class="pull-left">
        <input type="submit" id="removeAssignment" class="btn btn-danger" value="Remove Assignment">
        <input type="hidden" name="TheAssignment" id="removeAssignmentID" value="">
    </div>
</form>

那是应该提交 ID 的表单,所以我会知道要删除什么。但是,当我提交表单时,没有设置任何值。

请注意:上面的 JS 代码可能缺少一个 } 或两个,因为它只是一个示例。

4

1 回答 1

5

这里有一个例子:

$("select[name=test]").change(function(){
 $("input[name=hiddenfield]").val($(this).val());
});

http://jsfiddle.net/Wyp8f/

它会随着选择字段的每次更改而更改隐藏字段的值。您可以测试代码。这是从下拉列表中为隐藏字段分配值的方式。也许它可以帮助你。

于 2013-06-10T15:53:01.440 回答