<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <!-- input_1 자리에 input_{post.id} 들어가면됨 -->
    <input type="text" id="input_1" value="test" />

    <!-- 1 자리에 {post.id} 들어가면됨 -->
    <button class="1 123" onclick="test()">클릭</button>

    <script>

        let test = function(){

            // event.target.className  - event.target은 누른 버튼을 의미
            // 그 버튼에서 className을 가져온다
            console.log("버튼의 클래스는 " + event.target.className.split(" ")[0]);

            //"#input_"+ event.target.className 은 input_1 과 마찬가지다
            let data ={
                input1 : $("#input_"+ event.target.className.split(" ")[0]).val()
            }

            console.log(data.input1);
        }

    </script>
</body>
</html>

 

+ Recent posts