如果选中复选框,则将一组输入字段设置为禁用
Set a group of input fields to be disabled if a checkbox is checked
我正试图编写一个wordpress小部件,但选项表单让我有些悲伤。该小部件旨在显示两个图像和每个图像下方的计数器,计数器依赖于日期。这种逻辑是合理的。
然而,在图像上没有计数器的情况下,我试图设置一个复选框,当选中该复选框时,将禁用处理图像和计数器的输入字段,这样我就可以读取布尔值并输出一个没有计数器的简单图像。
我遇到的问题是,附加在复选框上的JQuery正在禁用输入,但我无法再次单击它来启用它们,因为它只是锁定在边缘状态,因为复选框会发出蓝色的"辉光",看起来没有被选中。我不明白为什么代码不能像我预期的那样工作。值得注意的是,这是我第一次使用JQuery,如果标准javascript也能做到这一点,那么解决方案就不需要是JQuery。
我有两个功能相同的部分,每个图像计数器对一个。这是其中一个部分的代码:
<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>
JQuery
jQuery(function($){
$(document).on('click', '#rand1', function(evt){
checked = $('#rand1').val();
$('div.ani1 :input').attr('disabled',checked);
return false;
});
});
编辑:自从发布后,我修改了代码,现在我使用id而不是类,但问题仍然存在,即使使用3ror404的解决方案
新代码:
<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
$('#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
});
我根据以下声明运行JQuery 1.11.1版本:
alert(jQuery.fn.jquery);
如果我理解正确,您希望在选中#rand1
时禁用.ani1
中的所有输入,如果未选中则启用(?)。所以这应该有效:
$('#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>
相关文章:
- $q.all当输入数组中的一项不是promise时,Typescript检查器失败
- 如何在一个表的输入中搜索一组特定的值,而不是另一个表's id(s)值以添加css
- 使用一组大的输入框进行表单验证
- 尝试使用 jQuery 序列化() 一组输入字段
- 组合功能以使用 Jquery 在一组单选按钮中显示输入字段和单选按钮
- 对两个不同的输入重复一组摩卡测试
- 当输入发生更改时,从一组输入中删除内容
- jquery表单验证需要一组输入中的一个,并要求输入是整数
- 如果选中复选框,则将一组输入字段设置为禁用
- 在交互方面,不要允许文本输入中的第一个字符是一个空格或一组空格
- 将输入字段中的字符限制为一组字符
- 基于一个值对一组输入使用ng-disabled
- 如何使用html input required属性将一组输入设置为required ?
- 如何获取输入字段,根据一组规则过滤输入并应用于另一个字段
- 检查我的用户输入的是一组长度的数字
- 如何使用jquery防止用户取消一组复选框输入中的所有复选
- jquery的一组输入值乘以dynamic(另一个输入)
- 遍历嵌套到组和节中的一组输入
- 如何在填写现有字段后在表单中添加一组输入字段
- 在js表单上,如何隐藏/显示单选按钮(一组3个按钮中的一个或两个)取决于在上面字段中输入的数字的值