简体   繁体   English

如何修复wordpress小部件插件中的jquery点击事件?

[英]How to fix jquery click event in wordpress widget plugin?

I'm creating wordress widget plugin...there are 4 checkboxes in my widget...when i click on first check box that will hide other 3 checkboxes.the problem is jquery's click event that is not working.我正在创建 wordress 小部件插件……我的小部件中有 4 个复选框……当我单击第一个复选框时,将隐藏其他 3 个复选框。问题是 jquery 的单击事件不起作用。 also there is no error in console.控制台也没有错误。 when i click "Enable accessibility mode" in wordpress admin then click event works fine...how do i fix that issue?当我在 wordpress 管理中单击“启用辅助功能模式”然后单击事件工作正常...我该如何解决该问题?

Here is PHP plugin code:这是PHP插件代码:

function include_jscript() {
    wp_enqueue_script('myscript', plugins_url('js/script.js',__FILE__ ),array('jquery'));
}
add_action( 'admin_enqueue_scripts', 'include_jscript' );

Here is jquery code:这是jquery代码:

jQuery(document).ready(function($){ 
$('#chk_site').on('click',function(){
        if($(this).prop("checked") == true)
            $("#chkboxes").hide();
        else
            $("#chkboxes").show();
    }); 
});

 <div class="widget-content"> <br><label style="display:block;margin-bottom:6px;"><input type="checkbox" id="chk_site" class="widget-zsearch-2-chk_site" name="widget-zsearch[2][chk_site]" value="">Display post type on site</label><div id="chkboxes"><label style="display:block;margin-bottom:6px;"><input type="checkbox" id="chk_post" class="widget-zsearch-2-post" name="widget-zsearch[2][chk_post]" value="post">Search in Posts</label><label style="display:block;margin-bottom:6px;"><input type="checkbox" id="chk_page" class="widget-zsearch-2-page" name="widget-zsearch[2][chk_page]" value="page">Search in Pages</label><label style="display:block;margin-bottom:6px;"><input type="checkbox" id="chk_attachment" class="widget-zsearch-2-attachment" name="widget-zsearch[2][chk_attachment]" value="attachment">Search in Attachments</label><br></div> </div>

There are some syntax errors in your code.您的代码中有一些语法错误。 For example, this is the correct syntax of a js if/else statement if(argument){}else{} .例如,这是 js if/else 语句if(argument){}else{}的正确语法。 Here is a working solution,这是一个有效的解决方案,

 $(function(){ $('.chkboxes').on('click', function() { if ($(this).is(":checked")){ $(this).removeClass('chkboxes'); $(".chkboxes").hide(); } else{ $(this).show(); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <input class='chkboxes' type="checkbox"/> <input class="chkboxes" type="checkbox"/> <input class="chkboxes" type="checkbox"/> <p> click any box to hide the others </p>

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

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