简体   繁体   English

更改后如何只提交表单上单个选择框的值?

[英]How to submit only the value of a single select box on a form when changed?

I am working on a project that loads a list of items and the user needs to be able to change attributes to this list of items. 我正在一个项目中加载项目列表,并且用户需要能够将属性更改为该项目列表。

Basically I have 50 select boxes on a form with pre-loaded selections. 基本上,我在具有预加载选择的表单上有50个选择框。 I want to submit only the value of the select box that the user chooses to change. 我只想提交用户选择更改的选择框的值。 I know I know I need to use the on change attribute and have it submitted. 我知道我需要使用on change属性并将其提交。 However how do I only submit the value of the individual element changed? 但是,我如何只提交已更改的单个元素的值?

One quick'n'dirty approach would be to wrap each individual select box in their own form element with each form having a unique POST action. 一种快速的处理方法是将每个单独的选择框包装在自己的表单元素中,每个表单具有唯一的POST操作。

A cleaner approach would be to have a hidden form that contains a hidden field for the selectBox the user changed and a hidden field for the value they changed it to. 一种更干净的方法是拥有一个隐藏的表单,其中包含用户更改的selectBox的隐藏字段和包含用户将其更改为的值的隐藏字段。 The workflow would be: User changes select box -> Update hidden fields -> Submit hidden form. 工作流程为:用户更改选择框->更新隐藏字段->提交隐藏表单。

OnChange use Ajax to send just the value of the select box to your endpoint using GET. OnChange使用Ajax使用GET将选择框的值仅发送到端点。

Assuming Jquery (but any library or native JS will work) 假设使用Jquery(但任何库或本机JS均可使用)

Some dirty code (time constraints) to get you started: 一些肮脏的代码(时间限制)可以帮助您入门:

var selVal = $("#myselect").val();   
$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "url-that-will-handle-the-data",
    data: '{ "param1": selVal }',
    success: function (msg) {
        alert(msg.d);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM