Retrieve Button value with jQuery

后端 未结 9 1762
一个人的身影
一个人的身影 2020-11-27 14:08

A simple one, I\'m trying to retrieve the value attribute of a button when its been pressed using jQuery, here\'s what I have:



        
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 14:45

    Inspired by postpostmodern I have made this, to make .val() work throughout my javascript code:

    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

提交回复
热议问题