如何将null select2值发送到服务器?

时间:2022-11-27 19:23:09

I create a select2 for tags.

我为标签创建了一个select2。

<select class="form-control selec2-tags form-control" multiple="multiple" name="keywords[]" style="width: 100%"></select>

and js:

$(".selec2-tags").select2({
   tags: true,
   placeholder: "type keywords...",
   maximumSelectionLength: 5,
   allowClear: true
}); 

For example I create a new content with these keywords.

例如,我使用这些关键字创建新内容。

I submit form and data will send to server and save in DB.

我提交表单,数据将发送到服务器并保存在DB中。

But when I edit (update) the content and I want delete all keywords, the null value of select box does not send to server. Because it is null now!

但是当我编辑(更新)内容并且我想要删除所有关键字时,选择框的空值不会发送到服务器。因为它现在是空的!

I hope you underestand me.

我希望你不和我。

How can I manage it in client-side to send select value (null or full) always?

如何在客户端管理它以始终发送选择值(null或full)?

1 个解决方案

#1


0  

Have you tried to handle the 'change' event of select2?

您是否尝试过处理select2的“更改”事件?

$(".selec2-tags").on('change', function() {
   var value = $(".selec2-tags").val();
});

value will be either an array of tags or null when no tags are selected. Inside the handler you can make a call to the server to update your DB. See here to get the idea

value将是一个标记数组,或者在没有选择标记时为null。在处理程序内部,您可以调用服务器来更新数据库。看到这里得到的想法

#1


0  

Have you tried to handle the 'change' event of select2?

您是否尝试过处理select2的“更改”事件?

$(".selec2-tags").on('change', function() {
   var value = $(".selec2-tags").val();
});

value will be either an array of tags or null when no tags are selected. Inside the handler you can make a call to the server to update your DB. See here to get the idea

value将是一个标记数组,或者在没有选择标记时为null。在处理程序内部,您可以调用服务器来更新数据库。看到这里得到的想法