program story

jQuery를 사용하여 버튼 값 검색

inputbox 2021. 1. 5. 08:04
반응형

jQuery를 사용하여 버튼 값 검색


간단한 것, jQuery를 사용하여 버튼을 눌렀을 때 버튼의 값 속성을 검색하려고합니다.

<script type="text/javascript">
    $(document).ready(function() {
        $('.my_button').click(function() {
            alert($(this).val());
        });
    });
</script>

<button class="my_button" name="buttonName" value="buttonValue">
    Button Label</button>

Firefox에서 내 경고는 'buttonValue'를 표시하지만 IE7에서는 '버튼 레이블'을 표시합니다.

버튼의 값을 항상 얻으려면 어떤 jQuery를 사용해야합니까? 아니면 다른 접근 방식을 사용해야합니까?

감사합니다.

답변 : 저는 지금

<input class="my_button" type="image" src="whatever.png" value="buttonValue" />

나는 이것이 얼마 전에 게시되었음을 알고 있지만 누군가가 답변을 찾고 있고 실제로 입력 요소 대신 버튼 요소를 사용하고 싶은 경우 ...

당신은 할 수 사용 .attr('value')또는 .val()IE에서 버튼. IE는 .val () 및 .attr ( "value")를 value 속성의 실제 값 대신 버튼 요소의 텍스트 레이블 (내용)로보고합니다.

버튼의 레이블을 일시적으로 제거하여 문제를 해결할 수 있습니다.

var getButtonValue = function($button) {
    var label = $button.text(); 
    $button.text('');
    var buttonValue = $button.val();
    $button.text(label);
    return buttonValue;
}

IE의 버튼에는 몇 가지 다른 단점이 있습니다. 여기에 가장 일반적인 두 가지 문제에 대한 수정 사항을 게시 했습니다 .


버튼 값은 속성이므로 jquery에서 .attr () 메서드를 사용해야합니다. 이것은 그것을해야한다

<script type="text/javascript">
    $(document).ready(function() {
        $('.my_button').click(function() {
            alert($(this).attr("value"));
        });
    });
</script>

attr을 사용하여 속성, 문서의 추가 정보를 설정할 수도 있습니다 .

이것은 JQuery 1.6 이상에서만 작동합니다. 이전 버전에 대한 포스트 모던의 답변을 참조하십시오.


버튼에 값 속성이 없습니다. 버튼의 텍스트를 얻으려면 :

$('.my_button').click(function() {
     alert($(this).html());
});

새로운 HTML5 사용자 정의 데이터 속성을 사용할 수도 있습니다.

<script type="text/javascript">
$(document).ready(function() {
    $('.my_button').click(function() {
        alert($(this).attr('data-value'));
    });
});
</script>

<button class="my_button" name="buttonName" data-value="buttonValue">Button Label</button>

버튼에 값 속성을 부여한 다음 다음을 사용하여 값을 검색합니다.

$("button").click(function(){
  var value=$(this).attr("value");
});

버튼에 이것을 시도하십시오.

<input type="button" class="my_button" name="buttonName" value="buttonValue" />

포스트 포스트 모던에서 영감을 받아 .val ()이 내 자바 스크립트 코드 전체에서 작동하도록 만들었습니다.

jQuery(function($) {

    if($.browser.msie) {
        // Fixes a know issue, that buttons value is overwritten with the text

        // Someone with more jQuery experience can probably tell me
        // how not to polute jQuery.fn here:
        jQuery.fn._orig_val = jQuery.fn.val

        jQuery.fn.val = function(value) {
            var elem = $(this);
            var html
            if(elem.attr('type') == 'button') {
                // if button, hide button text while getting val()
                html = elem.html()
                elem.html('')
            }
            // Use original function
            var result = elem._orig_val(value);
            if(elem.attr('type') == 'button') {
                elem.html(html)
            }
            return result;
        }
    }
})

It does however, not solve the submit problem, solved by postpostmodern. Perhaps this could be included in postpostmodern's solution here: http://gist.github.com/251287


if you want to get the value attribute (buttonValue) then I'd use:

<script type="text/javascript">
    $(document).ready(function() {
        $('.my_button').click(function() {
            alert($(this).attr('value'));
        });
    });
</script>

 <!DOCTYPE html>
 <html>
 <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
 </script>
 <script>
 $(document).ready(function(){
   $("#hide").click(function(){
    $("p").toggle();

    var x = $("#hide").text();

      if(x=="Hide"){
      $("button").html("show");

      }
     else 
     {
     $("button").html("Hide");

      }

       });

   });

  </script>
  </head>
  <body>

 <p>If you click on the "Hide" button, I will disappear.</p>

 <button id="hide">Hide</button>



 </body>
 </html>

ReferenceURL : https://stackoverflow.com/questions/487056/retrieve-button-value-with-jquery

반응형