简体   繁体   English

如果选中复选框,则将一组输入字段设置为禁用

[英]Set a group of input fields to be disabled if a checkbox is checked

I'm trying to write a wordpress widget but the options form is giving me some grief. 我正在尝试编写wordpress小部件,但选项表给我带来了一些麻烦。 The widget is designed to show an two images and a counter beneath each where the counter relies on the date. 该小部件旨在显示两个图像以及每个图像下方的一个计数器,其中计数器依赖于日期。 That logic is sorted. 该逻辑已排序。

However in a case where there is to be no counter on an image, i'm trying to put a checkbox which when checked will disable the input fields dealing with the image and counter so i can jsut read a boolean and output a simpler image with no counter. 但是,在图像上没有计数器的情况下,我尝试放置一个复选框,当选中该复选框时,该复选框将禁用处理图像和计数器的输入字段,因此我可以通过jsut读取布尔值并使用来输出更简单的图像没有柜台。

The problem I'm having is that the JQuery that's attached to the checkbox is disabling the inputs, but i can't click it again to enable them as it just locks in its state of limbo as the checkbox gains a blue 'glow' and doesn't look checked. 我遇到的问题是,附加到复选框的JQuery禁用了输入,但是我无法再次单击它来启用它们,因为当复选框获得蓝色“辉光”时,它只是处于其边缘状态,看起来没有检查。 I don't get why the code isn't working as i expect. 我不明白为什么代码无法按我期望的那样工作。 It's worth noting that this is my first time using JQuery, and the solution doesn't need to be JQuery if standard javascript can do the same. 值得注意的是,这是我第一次使用JQuery,并且如果标准javascript可以做到这一点,则解决方案不必是JQuery。

I have 2 sections which will be identical in functionality, one for each image-counter pair. 我有2个功能相同的部分,每个图像计数器对一个。 this is the code for one of those sections: 这是这些部分之一的代码:

<h3>Anime 1</h3>
    <p>
        <input class="check1" type="checkbox" <?php checked($instance['s1rand'], 'on'); ?> id="rand1" name="<?php echo $this->get_field_name('s1rand'); ?>" /> 
        <label for="<?php echo $this->get_field_id('s1rand'); ?>">Label of your checkbox variable</label>
    </p>
<div class="ani1">
<p>
<label for="<?php echo $this->get_field_id('s1title'); ?>"><?php _e('Anime 1 Name:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('s1title'); ?>" name="<?php echo $this->get_field_name('s1title'); ?>" type="text" value="<?php echo $s1title; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('s1date'); ?>"><?php _e('Anime 1 Start Date:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('s1date'); ?>" name="<?php echo $this->get_field_name('s1date'); ?>" type="date" value="<?php echo $s1date; ?>" />
</p>
</div>

The JQuery jQuery的

jQuery(function($){

  $(document).on('click', '#rand1', function(evt){
    checked = $('#rand1').val();
    $('div.ani1 :input').attr('disabled',checked);
    return false;
  });
});

EDIT: Since posting i have altered the code so that i now work with ids rather than classes, but the problem persists, even when using 3rror404's solution 编辑:自从发布以来,我已经更改了代码,以便我现在可以使用id而不是类,但是即使使用3rror404的解决方案,问题仍然存在

New code: 新代码:

<h3>Anime 1</h3>
<p>
    <input class="check1" type="checkbox" <?php checked($instance['s1rand'], 'on'); ?> id="rand1" name="<?php echo $this->get_field_name('s1rand'); ?>" /> 
    <label for="rand1">Label of your checkbox variable</label>
</p>
<div id="ani1">
<p>
    <label for="s1title"><?php _e('Anime 1 Name:', 'wp_widget_plugin'); ?></label>
    <input class="widefat" id="s1title" name="<?php echo $this->get_field_name('s1title'); ?>" type="text" value="<?php echo $s1title; ?>" />
</p>
<p>
    <label for="s1date"><?php _e('Anime 1 Start Date:', 'wp_widget_plugin'); ?></label>
    <input class="widefat" id="s1date" name="<?php echo $this->get_field_name('s1date'); ?>" type="date" value="<?php echo $s1date; ?>" />
</p>
</div>

JQuery jQuery查询

$('#rand1').on('change',function() {
  var isChecked = $(this).prop('checked'); // this will equate to either 'true' or 'false'...
  alert(isChecked); //this line doesn't work, which makes me think this function isn't working
  $('#ani1 input').attr('disabled',isChecked); // ...meaning that we can use its value to set the 'disabled' attribute 
});

I'm running JQuery version 1.11.1 according to the following statement: 我根据以下语句运行JQuery版本1.11.1:

alert(jQuery.fn.jquery);

If I understand correctly, you want all of the inputs within .ani1 to be disabled if #rand1 is checked and enabled if not checked(?). 如果我理解正确,那么您希望如果#rand1被选中,则禁用.ani1所有输入,如果未选中,则启用所有输入。 So this should work: 所以这应该工作:

 $('#rand1').on('change',function() { var isChecked = $(this).prop('checked'); // this will equate to either 'true' or 'false'... $('div.ani1 input').attr('disabled',isChecked); // ...meaning that we can use its value to set the 'disabled' attribute }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h3>Anime 1</h3> <p> <input class="check1" type="checkbox" id="rand1" name="" /> <label for="rand1">Label of your checkbox variable</label> </p> <div class="ani1"> <p> <label for="input1">label</label> <input class="widefat" id="input1" type="text" /> </p> <p> <label for="input2"></label> <input class="widefat" id="input2" type="date" /> </p> ... </div> 

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

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