第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JavaScript 對(duì)象數(shù)組插入新對(duì)象和控制臺(tái)日志

JavaScript 對(duì)象數(shù)組插入新對(duì)象和控制臺(tái)日志

PHP
ITMISS 2023-06-18 15:56:46
我正在嘗試制作一個(gè)動(dòng)態(tài)對(duì)象數(shù)組(如鍵:值)。我想插入每個(gè)鍵(被點(diǎn)擊的類名)和值(選中的數(shù)量)。我寫了 console.log 以便我可以從 PHP 中的數(shù)組中獲取值。問題是:它不會(huì)以那種方式插入新對(duì)象。var countChecked = function() {    var checked = {};    var n = $( '.'+ this.className + ':checked' ).length;    checked.push({'this.className',n});    console.log(checked);    alert( n + (n === 1 ? " is" : " are") + " checked!" );};countChecked();$( "input[type=checkbox]" ).on( "click", countChecked );
查看完整描述

1 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

看看它是否對(duì)你有幫助。

let checkedArr = []; // initialize the variable(type = array) to global scope.


function b(e) { // called every time a the checkbox is clicked


? let totalChecked = 0; // initialize the variable with 0;


? $("input[type=checkbox]." + e).each(function() { // check every checkbox with class e(e has className)


? ? //console.log($(this));

? ? if ($(this).is(":checked")) { // check if checkboxed is checked


? ? ? totalChecked++; // increment every time


? ? }

? });


? // find if class already exists -- returns the index of matched element and -1 for no match

? let existIndex = checkedArr.findIndex((item) => item.class == e);


? console.clear();

? // console.log(existIndex);


? if (existIndex != -1) { // if class exists in array, update its value


? ? checkedArr[existIndex].checked = totalChecked;


? } else { // if not insert the value as an object


? ? checkedArr.push( {class: e, checked: totalChecked} );

??

? }

??

? $('.some_class').val(JSON.stringify(checkedArr)); // convert it to string and assign it to the input field?

??

? alert($('.some_class').val());

? console.log(checkedArr);

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input onchange="b(this.className)" class="kaka" type="checkbox" name="worker_name[]" value="kaka1" />kaka1

<input onchange="b(this.className)" class="kaka" type="checkbox" name="worker_name[]" value="kaka2" />kaka2

<input onchange="b(this.className)" class="kaka" type="checkbox" name="worker_name[]" value="kaka3" />kaka3

&emsp;&emsp;


<input onchange="b(this.className)" class="lala" type="checkbox" name="worker_name[]" value="lala1" />lala1

<input onchange="b(this.className)" class="lala" type="checkbox" name="worker_name[]" value="lala2" />lala2

&emsp;&emsp;


<input onchange="b(this.className)" class="jaja" type="checkbox" name="worker_name[]" value="jaja1" />jaja1

<input onchange="b(this.className)" class="jaja" type="checkbox" name="worker_name[]" value="jaja2" />jaja2


<!-- Make this ↓↓ hidden -- here only for demo -->

<input type="text" name="hidden_field" class="some_class" value="" />

現(xiàn)在您可以看到,輸入字段的值為checkeArr。然后,在您的中,controller您可以獲得的價(jià)值為 -


$hidden = $this->input->post('hidden_field'); // name of your hidden field

$hidden = json_decode($hidden); // convert it back to its original form


print_r($hidden);

輸出:


Array

(

? ? [0] => stdClass Object

? ? ? ? (

? ? ? ? ? ? [class] => kaka

? ? ? ? ? ? [checked] => 1

? ? ? ? )


? ? [1] => stdClass Object

? ? ? ? (

? ? ? ? ? ? [class] => lala

? ? ? ? ? ? [checked] => 1

? ? ? ? )


? ? [2] => stdClass Object

? ? ? ? (

? ? ? ? ? ? [class] => jaja

? ? ? ? ? ? [checked] => 2

? ? ? ? )


)

您現(xiàn)在可以foreach根據(jù)需要使用它來獲取其價(jià)值。


查看完整回答
反對(duì) 回復(fù) 2023-06-18
  • 1 回答
  • 0 關(guān)注
  • 124 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)