如果在页面加载和单击时勾选了复选框,则禁用输入

时间:2015-07-08 10:53:25

标签: javascript jquery checkbox input disabled-input

我已经查看过大量问题,这些问题描述了如何检查天气是否勾选了复选框,但是没有一个问题能够提供我需要的答案。我想检查是否在页面加载时选中了复选框,如果是,则禁用输入。然后,我需要在页面加载后选中/取消选中该框时更改禁用的输入。

这就是我所拥有的:

jQuery(document).ready(function($) {
    if ($('.is-repeat-event').prop('checked')) {
      $('.event-date').prop('disabled', 'true' );
    }
    $('.is-repeat-event').change(function(){
        var d = this.checked ? 'true' : 'false';
        $('.event-date').prop('disabled', d );
    });
});

要么是它自己的作品的一部分,但把它们放在一起不起作用(.change事件对输入没有影响)。

2 个答案:

答案 0 :(得分:2)

这是因为禁用属性需要设置为布尔值true / false值。您也可以在绑定后触发事件以查看页面加载中的相关更改:

# x contains x coords
# y contains y coords
# angle contains the current moving direction (radian)
import numpy as np

phi = np.pi/2 + angle
x_left = x + dist*np.cos( phi )
y_left = y + dist*np.sin( phi )

x_right = x - dist*np.cos( phi )
y_right = y - dist*np.sin( phi )

x_total = hstack((x_left, x_right[::-1]))
y_total = hstack((y_left, y_right[::-1]))        

##----- Omit Points with minimal dist < threshold to ANY point on traj
x_res = []
y_res = []
for idx in range(len(x_total)):
    m = np.min( np.sqrt( (x-x_total[idx])**2 + (y-y_total[idx])**2) )
    if m > dist-epsilon:
        x_res.append( x_total[idx] )
        y_res.append( y_total[idx] )    
points = np.vstack( (x_res, y_res) ).T

答案 1 :(得分:0)

尝试使用trigger()

$(function() {
  $('.is-repeat-event').on('change', function(e) {
    $('.event-date').prop('disabled', this.checked);
  }).trigger('change');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
  <label>Repeat
    <input type='checkbox' class="is-repeat-event" checked='' />
  </label>
</div>
Event Date
<input type='date' class="event-date" value="2015-07-12" />