英文:
jQuery change event not firing on page load, how can I fix it?
问题
这是您的代码:
$awaySelect = $('#awayID');
$awaySelect.change(function(){
      alert('hi'); 
      var awayData = $(this).val();
      if(awayData == 202 || awayData == 203){ 
            $('.gameToshowAway').show(); 
            $('.gameToshowAway').css('display','inline-block');
            $("#gameDate3").prop('disabled',false); 
            $('.gamedatecolor1').css('display','inline-block'); 
            $('.positionToDisplayAway').hide();                   
        } else {
        	$('#unknownWell').hide();
            $('.gameToshowAway').hide();           
        } 
        if(awayData == 204) {
            $('.gameToshowAway').hide();  
            $('#unknownWell').hide();
            $('.positionToDisplayAway').css('display','inline-block');          
        }
    }).trigger('change');
请注意,我已经将你的代码中的##改为了#,因为ID选择器应该使用单个#。另外,我将.trigger()的参数改为了'change'以触发change事件。
希望这对您有所帮助,如果还有其他问题,请随时提出。
英文:
I have a code which is used in both purpose, even on click of any other button and even while editing, it should do on page load
Here is my code
$awaySelect = $('##awayID');
$awaySelect.change(function(){
      alert('hi'); 
      var awayData = $(this).val();
      if(awayData == 202 || awayData == 203){ 
            $('.gameToshowAway').show(); 
            $('.gameToshowAway').css('display','inline-block');
            $("##gameDate3").prop('disabled',false); 
            $('.gamedatecolor1').css('display','inline-block'); 
            $('.positionToDisplayAway').hide();                   
        } else {
        	$('##unknownWell').hide();
            $('.gameToshowAway').hide();           
        } 
        if(awayData == 204) {
            $('.gameToshowAway').hide();  
            $('##unknownWell').hide();
            $('.positionToDisplayAway').css('display','inline-block');          
        }
    }).trigger();
but it not doing any alert even i had the trigger at the bottom to try to execute it on page load
what is wrong happening here
答案1
得分: 0
使用.change()而不带参数,或将事件名称传递给.trigger。
$awaySelect.change(function(e) {
    // ...
}).change();
// 或者
$awaySelect.trigger('change');
英文:
Call .change() with no arguments or pass the event name to .trigger.
$awaySelect.change(function(e) {
    // ...
}).change();
// or
$awaySelect.trigger('change');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论