jquery로 태그 이벤트안의 함수로 인자를 전달할 수 있나요?

jquery로 태그 이벤트안의 함수로 인자를 전달할 수 있나요?

QA

jquery로 태그 이벤트안의 함수로 인자를 전달할 수 있나요?

본문

안녕하세요.

아래는 selectA으 옵션을 선택해서 selectB로 값을 넘기고 있습니다.

그런데

이와 함께

그 값을 span태그안의 onclick함수 sample에 넘기고 싶습니다.

 

가능한가요?

 

 

<script>

$(document).ready(function(){

    $('#selectA').change(function(e){

        var thisVal = $("#selectA option:selected").val();

        var thisText = $("#selectA option:selected").text();

 

        $('#selectB').empty();

        $('<option>').text(thisText).val(thisVal).appendTo('#selectB');

    });

   

})

 

function sample(a){

 

console.log(a);

 

}

</script>

</head>

 

<body>

    <select name="selectA" id="selectA" >

        <option value="">선택</option>

        <option value="1">1번</option>

        <option value="2">2번</option>

    </select>

 

  <select name="selectB" id="selectB" ></select>

 

   

    <span id="please" onclick="sample(a)">mybutton</span>

이 질문에 댓글 쓰기 :

답변 1

자문자답)

네이버 지식인에도 질문을 올렸더니 답변이 있어서 여기에 올립니다.

 

<script>

$(document).ready(function(){

    $('#selectA').change(function(e){

        var thisVal = $("#selectA option:selected").val();

        var thisText = $("#selectA option:selected").text();

 

        $('#selectB').empty();

        $('<option>').text(thisText).val(thisVal).appendTo('#selectB');

        $("#please").attr("onclick","sample('" + thisVal + "')");

    });

})

 

function sample(a){

    console.log(a);

}

</script>

</head>

 

<body>

    <select name="selectA" id="selectA" >

        <option value="">선택</option>

        <option value="1">1번</option>

        <option value="2">2번</option>

    </select>

 

<select name="selectB" id="selectB" ></select>

<span id="please" _onclick="sample(a)">mybutton</span>

위의 코드처럼 $("#please").attr("onclick","sample('" + thisVal + "')"); 로 처리 할 수 있습니다. 말씀하신 내용이 맞는지 확인해보세요.

답변을 작성하시기 전에 로그인 해주세요.
전체 123,663 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT