亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以 php 形式從多個選擇中發布未選擇的選項

以 php 形式從多個選擇中發布未選擇的選項

PHP
躍然一笑 2023-05-12 14:51:37
我可以在 PHP 中獲取我的 selectedFruit 數組,但是如何使用 js 或 jquery 填充隱藏的輸入以便我可以實現我的目標?<form id="my_form" action method="post"><select name="selectedFruit[]" multiple="multiple">    <option value="1">Orange</option>    <option value="2">Grape</option>    <option value="3">Apple</option>    <option value="4">Watermelon</option></select><input hidden name="unselectedFruit[]"><button type="submit">Submit</button></form>我的目標是在我的 php 文件中執行此操作:$selected_array_fruit = $_POST["selectedFruit[]"];$unselected_array_fruit = $_POST["unselectedFruit[]"];
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

如果更改了選擇,您可以創建新的輸入值。


- 創建函數,從 select 中過濾未選擇的值,并使用 unselectedFruit[] 名稱為 PHP 數組創建新的隱藏輸入。

- 為更改創建偵聽器并重新計算選擇選項。


// this is for explain and by default is truncated and filtered by selector

// option:selected and remove the line 'skip selected'

const loadSelectUncheckedValues = function() {

  $('[name="unselectedFruit[]"]').remove(); // clean all

  $('select[name="selectedFruit[]"] option').each(function() {

    if($(this).is(':selected')) return; // skip selected

    $('#my_form').append('<input type="hidden" name="unselectedFruit[]" value="'+$(this).val()+'" />');

  });

}


// init

loadSelectUncheckedValues();


$('select[name="selectedFruit[]"]').on('change', function() {

    loadSelectUncheckedValues();

});


查看完整回答
反對 回復 2023-05-12
  • 1 回答
  • 0 關注
  • 113 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號