How do I get javascript to run more than once?

2019-08-14 08:01发布

EDIT: here is a jsfiddle with all the code. https://jsfiddle.net/ypbd3fgf/2/

I'm using this simple code to add a class to a parent element in order to make the text on a selected radio button turn bold. However, it only works on page load. If you select different radio buttons, the newly selected buttons don't turn bold. I think it's because the code only runs on page load. How do I get it to update when a new radio button or checkout is selected? Thanks!

<script type="text/javascript"> 
$(function() {
        $(".ez-selected").closest('.row').addClass("bold");
}); 
</script>

The is the HTML. The .ez-selected class is added to the .ez-radio div when a radio button is selected. And then removed when a different radio button is selected. When the .ez-selected class is added, the .bold class needs to be added to the .row div. When the .ez-selected class is removed the .bold class needs to be removed from the .row div.

  <div class="row (bold)">
        <input name="TXT870" type="hidden" value="Option 1">
        <div class="col-xs-2">
            <div class="ez-radio (ez-selected)">
                <input class="clearBorder ez-hide" name="CAG3" onclick=
                "javascript:document.additem.CAG3QF1.value='1'; CheckPreValue(this, 2, 0);"
                type="radio" value="870_625_0_625">
            </div><input name="CAG3QF1" type="hidden" value="0">
        </div>
        <div class="col-xs-7">
            <span>Option 1</span> <input class="transparentField" name=
            "CAG3TX1" readonly size="14" type="text" value=" - Add $5.00">
        </div>
    </div>

EDIT: I added the JS code below that adds and removes the .ez-selected class. This new JS that I'm trying to make will simply be adding and removing the bold class when the .ez-selected class is added and removed by this other JS code below.

(function($) {
    $.fn.ezMark = function(options) {
        options = options || {};
        var defaultOpt = {
            checkboxCls: options.checkboxCls || 'ez-checkbox',
            radioCls: options.radioCls || 'ez-radio',
            checkedCls: options.checkedCls || 'ez-checked',
            selectedCls: options.selectedCls || 'ez-selected',
            hideCls: 'ez-hide'
        };
        return this.each(function() {
            var $this = $(this);
            var wrapTag = $this.attr('type') == 'checkbox' ?
                '<div class="' + defaultOpt.checkboxCls + '">' :
                '<div class="' + defaultOpt.radioCls + '">';
            if ($this.attr('type') == 'checkbox') {
                $this.addClass(defaultOpt.hideCls).wrap(wrapTag)
                    .change(function() {
                        if ($(this).is(':checked')) {
                            $(this).parent().addClass(
                                defaultOpt.checkedCls);
                        } else {
                            $(this).parent().removeClass(
                                defaultOpt.checkedCls);
                        }
                    });
                if ($this.is(':checked')) {
                    $this.parent().addClass(defaultOpt.checkedCls);
                }
            } else if ($this.attr('type') == 'radio') {
                $this.addClass(defaultOpt.hideCls).wrap(wrapTag)
                    .change(function() {
                        $('input[name="' + $(this).attr(
                            'name') + '"]').each(
                            function() {
                                if ($(this).is(
                                    ':checked')) {
                                    $(this).parent().addClass(
                                        defaultOpt.selectedCls
                                    );
                                } else {
                                    $(this).parent().removeClass(
                                        defaultOpt.selectedCls
                                    );
                                }
                            });
                    });
                if ($this.is(':checked')) {
                    $this.parent().addClass(defaultOpt.selectedCls);
                }
            }
        });
    }
})(jQuery);

2条回答
Fickle 薄情
2楼-- · 2019-08-14 08:50

Edit: The new code switches to the function(){...} format and looks for a click event on an input element then checks if its a radio button and that the parent is .row then it adds the bold and ez-selected classes to the radio button and removes them from other radios.

$("input").click(function() {
    if($(this).attr('type') == 'radio' && $(this).parents('.row').length > 0){
        $("input").closest('.row').removeClass('bold ez-selected');
        $(this).addClass('bold ez-selected');
     }
})
查看更多
Evening l夕情丶
3楼-- · 2019-08-14 08:50

your code should look somewhat like this:

<script type="text/javascript"> 
    $(function() {
        $("input[type='radio']").click(function(e) {
            var targ = e.target
            $("div[class='row'][class='bold']).removeClass('bold');
            $(targ).parents('div[class="row"]').addClass('bold');
        })

        $(".ez-selected").closest('.row').addClass("bold");
    }); 
</script>
查看更多
登录 后发表回答