ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바스크립트 변수에 JQuery 값 전달 [중복]
    카테고리 없음 2020. 8. 10. 13:17

    질문

    나는 jquery와 javascript를 처음 사용합니다. id = "val" 값을 자바스크립트의 변수 scrt_var에 전달하고 싶습니다. 하지만 내 코드는 undefined 값을 반환합니다.

    내 코드는 다음과 같습니다.

    <script>
    $('.value-plus').on('click', function () {
    var divUpd = $(this).parent().find('.value'), newVal = parseInt(divUpd.text(), 10) + 1;
    divUpd.text(newVal);
    });
    
    $('.value-minus').on('click', function () {
    var divUpd = $(this).parent().find('.value'), newVal = parseInt(divUpd.text(), 10) - 1;
    if (newVal >= 1)
    divUpd.text(newVal);
    });
    </script>
    
    <script>
    var scrt_var = document.getElementById("val").value;
    </script>
    
    <div class="entry value-minus">&nbsp;</div>
    <div id="val" class="entry value"><span>1</span></div>
    <div class="entry value-plus active">&nbsp;</div>
    
    
    <p><a class="item_add" href="addToCart.jsp?orderdetail=<%=orderId%>,<%=i.getItemID()%>,<%=c.getCategoryID()%>,<%=i.getPrice()%>," onclick="location.href = this.href + scrt_var;return false;">add to cart </a></p>

    Output: addToCart.jsp?orderdetail=107,10,15,23.0,undefined

     

     

    답변1

    div의 경우 :

    
    scrt_var = $('#val').text();

    입력 상자의 경우 :

    scrt_var = $('#val').val();

    val은 id로 선택하기에 큰 가치가 아닙니다.



     

     

     

     

    출처 : https://stackoverflow.com/questions/63261845/pass-jquery-value-to-javascripts-variable

    댓글

Designed by Tistory.