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

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

如何從ajax中的輸入中獲取數組中的檢查值

如何從ajax中的輸入中獲取數組中的檢查值

PHP
喵喵時光機 2022-09-12 10:13:51
im試圖從我的ajax中獲取我檢查的輸入值,但它只返回我的第一個勾選...這是我的代碼function getBranchAjax(cuid, stid) {    $.ajaxSetup({        headers: {            'X-CSRF-Token': $('meta[name=csrf-token]').attr('content')        }    });    $.ajax({        url: "{{ route('reporting.getBranchAjax',['cuid','stid']) }}",        method: 'GET',        data: { cuid: cuid, stid: stid },        dataType: 'json',        success: function (data) {            console.log(data);            console.log(data.branch.length);            //remove all option except the first option the append with new option            //remove all option except the first option the append with new option            for (var x = 0; x < data.branch.length; x++) {                //console.log(data.branch[x].cb_branch_Name);                $('#brRecord').append(`<tr><td style='width: 110px; text-align: left;'>${data.branch[x].cb_branchcode}    </td><td style='width: 600px; text-align: left;'>${data.branch[x].cb_branch_Name}</td>     <td style='width: 20px;'>     <input type="checkbox" class="ss" name="ss" id="ss" value="${data.branch[x].cb_branch_Name}" /></td><td>                                    </td></tr>`);            }        },        fail: function (xhr, textStatus, errorThrown) {            alert('request failed');        }    })}按類名獲取值,這只是我第一次逐筆報價的返回值var checkedValue = null; var inputElements = document.getElementsByClassName('ss');for(var i=0; inputElements[i]; ++i){    if(inputElements[i].checked){        checkedValue = inputElements[i].value;        break;    }}console.log(checkedValue); // only return a value of my first tick我希望根據我的輸入價格變動值獲得更多價值
查看完整描述

2 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

多個值需要保存在數組(或類似數組)中。


例如:


var checkedValues = Array.from(document.getElementsByClassName('ss')).filter(el => el.checked).map(el => el.value);


console.log(checkedValues);

或者,由于問題被標記為 jQuery:


var checkedValues = jQuery('.ss:checked').get().map(el => el.value);


console.log(checkedValues);

使用數組中的值,您可以對它們執行任何操作,例如顯示它們或對它們執行一些轉換。


查看完整回答
反對 回復 2022-09-12
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

這是因為您在 for 循環中添加了一個中斷。


刪除它將解決您的問題。


for(var i=0; inputElements[i]; ++i){

        if(inputElements[i].checked){

            checkedValue = inputElements[i].value;

        }

    }

如果要獲取所有最新單擊的復選框,可以使用 onChange


function checkedInput() 

{

 cosnt checkedValue = [];

 for(var i=0; inputElements[i]; ++i){

        if(inputElements[i].checked){

            checkedValue.push(inputElements[i].value);

        }

    }

  return checkedValue ;


}


$(document).on('change', '#ss', function () {

 console.log(checkedInput());

})


查看完整回答
反對 回復 2022-09-12
  • 2 回答
  • 0 關注
  • 94 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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