简体   繁体   English

jQuery的触发方法在准备好文档后无法正常工作

[英]jQuery's trigger method doesn't work properly on document ready

I have some functionality that changes an HTML element based on the choice of the radio input, and that's working fine. 我有一些功能可以根据radio输入的选择来更改HTML元素,并且可以正常工作。 However, for the initial setup of the element, instead of duplicating the conditioning and the code again, it would be much shorter and clearer to just invoke the radio 's trigger('change') method which then does all the work. 但是,对于元素的初始设置,与其再次复制条件和代码,不如直接调用radiotrigger('change')方法来完成所有工作,它将变得更短,更清晰。 This doesn't seem to work properly (in Firefox or Chrome). 这似乎无法正常工作(在Firefox或Chrome中)。 As much as I figure, it always treats the last radio in the document as the checked one. 据我所知,它总是将文档中的最后一个radio视为已选中的radio

A sample is here: http://jsfiddle.net/jydf5gby/ 示例在这里: http : //jsfiddle.net/jydf5gby/

The initial color of the button should be red, but it comes up as blue. 按钮的初始颜色应该是红色,但是会变成蓝色。 Why is that, and what is the solution? 为什么会这样,解决方案是什么?

HTML: HTML:

<p><input type="radio" name="radio" value="red" checked="checked"/> red</p>
<p><input type="radio" name="radio" value="blue"/> blue</p>

<button>button</button>

Javascript: 使用Javascript:

$(document).ready(function () {
    $('input[name=radio]').change(function () {
        if ($(this).val() == 'red') {
            $('button').css('background', 'red');
        } else {
            $('button').css('background', 'blue');
        }
    }).trigger('change');
});

You're triggering the event on all the radio buttons, not just the selected one. 您正在所有单选按钮上触发事件,而不仅仅是选定的一个。 Try: 尝试:

 $(document).ready(function () { $('input[name=radio]').change(function () { if ($(this).val() == 'red') { $('button').css('background', 'red'); } else { $('button').css('background', 'blue'); } }).filter(":checked").trigger('change'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p><input type="radio" name="radio" value="red" checked="checked"/> red</p> <p><input type="radio" name="radio" value="blue"/> blue</p> <button>button</button> 

You can use it this way: 您可以通过以下方式使用它:

$(document).ready(function () {
    $('input[name=radio]').change(function () {
        $('button').css('background', $('input[name=radio]:checked').val());
    }).trigger('change');
});

Fiddle 小提琴


As you are triggering the event applied on the radio inputs so you can just apply the values of those radios directly as i put in the answer. 当您触发应用在radio输入上的事件时,您可以按照我在答案中所说的那样直接应用那些收音机的值。 This makes your code a bit smaller and you can save a bit of bytes. 这使您的代码更小,并且可以节省一些字节。

You have incorrect selector to target checked radio button value after triggering change event. 触发更改事件后,您没有正确的选择器定位已选中的单选按钮值。 you need to use: 您需要使用:

$('input[name=radio]').change(function () {
    if ( $('input[name=radio]:checked').val() == 'red') {
        $('button').css('background', 'red');
    } else {
        $('button').css('background', 'blue');
    }
}).trigger('change');

Working Demo 工作演示

Update - Optimized Answer by Rory McCrossan 更新-Rory McCrossan的优化答案

$(document).ready(function () {
  $('input[name=radio]').change(function () {
      $('button').css('background',  $('input[name=radio]:checked').val());
  }).trigger('change');
});

Demo for optimized code 演示优化代码

You are calling trigger for all the radio buttons. 您正在为所有单选按钮调用触发器。 You just need to filter() them to the :checked one: 您只需要将它们filter():checked一个:

$(document).ready(function () {
    $('input[name=radio]').change(function () {
        if ($(this).val() == 'red') {
            $('button').css('background', 'red');
        } else {
            $('button').css('background', 'blue');
        }
    }).filter(':checked').trigger('change');
});

See Fiddle here 在这里看到小提琴

You can also optimize your actual handler to just set the background-color to the current val(): 您还可以优化您的实际处理程序,仅将background-color设置为当前的val():

$(document).ready(function () {
    $('input[name=radio]').change(function () {
        $('button').css('background', $(this).val());
    }).filter(':checked').trigger('change');
});

Fiddle here 在这里摆弄

try this instead 试试这个代替

 $(document).ready(function () { $('input[name=radio]').bind("change",function(){ if ($(this).val() == 'red') { $('button').css('background', 'red'); } else { $('button').css('background', 'blue'); } }); $('input[name=radio]:first').click(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p><input type="radio" name="radio" value="red" /> red</p> <p><input type="radio" name="radio" value="blue"/> blue</p> <button>button</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM