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

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

如何在單擊時獲取復選框的值

如何在單擊時獲取復選框的值

PHP
元芳怎么了 2022-01-02 15:38:47
我有一個 html 表,每行都有一個動態創建的復選框。復選框的選中狀態是根據數據庫中的值設置的。當在 html 表中選中復選框時,我需要捕捉更改并設置值,以便如果選中該框,則 value=1 else value=0。值被傳遞給更新查詢。我不確定如何用我當前的代碼來完成這個。HTML:<?php     $sqlGetEmployees = "select e.employeeID, e.lastName, e.firstName, l.number, p.name, e.gradeCertLevel, e.serviceYears, e.step, e.certified from employee e join location l on l.locationID = e.locationID join position p on p.positionID = e.positionID";    $stmtGetEmployees = mysqli_prepare($db,$sqlGetEmployees);    mysqli_stmt_execute($stmtGetEmployees);    mysqli_stmt_bind_result($stmtGetEmployees, $id, $lastName, $firstName, $number, $name, $gradeCertLevel, $serviceYears, $salaryStep, $certified);    $count = 1;    while(mysqli_stmt_fetch($stmtGetEmployees))   {        $checkedCertified = ($certified == '1') ? 'checked="checked"':'';?>    <tr>        <!-- other table data -->        <td>             <div id='certified_<?php echo $id; ?>'>                <input contentEditable='true' class='edit' type='checkbox' id='certified' <?php echo $checkedCertified; ?> />            </div>         </td>    </tr><?php        $count ++;    }    mysqli_stmt_close($stmtGetEmployees);?>  腳本:    // Add Class    $('.edit').click(function(){        $(this).addClass('editMode');    });    // Save data    $(".edit").focusout(function(){        $(this).removeClass("editMode");        var id = this.id;        var split_id = id.split("_");        var field_name = split_id[0];        var edit_id = split_id[1];        var value = $(this).text();        $.ajax({            url: 'update.php',            type: 'post',            data: { field:field_name, value:value, id:edit_id },            success:function(response){                console.log('Save successfully');                           }        });    });
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

對于動態創建的元素,事件的處理方式如下:


$(document).on('click', '.edit', function(e) {

   //e.preventDefault();

   var checkedStatus = 0;

   if ($(this).is(':checked')) {

       $(this).addClass('editMode');

       checkedStatus = 1;

   } else {

       checkedStatus = 0;

       $(this).removeClass("editMode");

   }  


    var id = this.id;

    var split_id = id.split("_");

    var field_name = split_id[0];

    var edit_id = split_id[1];


    var value = $(this).text();


    $.ajax({

        url: 'update.php',

        type: 'post',

        data: { field:field_name, value:value, id:edit_id, checked: checkedStatus },

        success:function(response){

            console.log('Save successfully');               

        }

    });

});

要將所有內容放入單個事件處理程序中,您可以將所有內容放入 .click 或 .focusout 中,具體取決于您要實現的目標。


查看完整回答
反對 回復 2022-01-02
  • 1 回答
  • 0 關注
  • 210 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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