启用或禁用html中的复选框

时间:2012-08-16 13:24:23

标签: html jsp

我想根据条件启用或禁用表格行中的复选框。

代码 -

<td><input type="checkbox" name="repriseCheckBox" disabled={checkStat == 1 ? true : false}/></td>

如果checkStat = 1,则需要禁用复选框,否则启用它。

它不起作用。它会禁用所有复选框。 有什么建议吗?

6 个答案:

答案 0 :(得分:33)

如果您指定disabled属性,那么您为其提供的值必须disabled。 (在HTML 5中,您可以省略除属性值之外的所有内容。在HTML 4中,您可以省略除属性名称之外的所有内容。)

如果您不想禁用该控件,则根本不要指定该属性。

禁用:

<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">

启用:

<input type="checkbox">

无效(但通常会恢复错误以视为已禁用):

<input type="checkbox" disabled="1">
<input type="checkbox" disabled="true">
<input type="checkbox" disabled="false">

因此,在不知道您的模板语言的情况下,我猜您正在寻找:

<td><input type="checkbox" name="repriseCheckBox" {checkStat == 1 ? disabled : }/></td>

答案 1 :(得分:1)

HTML解析器根本不解释这样的内联JavaScript。

你可以这样做:

<td><input type="checkbox" id="repriseCheckBox" name="repriseCheckBox"/></td>

<script>document.getElementById("repriseCheckBox").disabled=checkStat == 1 ? true : false;</script>

答案 2 :(得分:1)

lazy var panGesture: UIPanGestureRecognizer = {
    let pan =  UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(sender:)))
        pan.delegate = self
    return pan
}()

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.backgroundColor = UIColor.cyan
    self.view.addSubview(collectionView)

    self.view.addConstraint(NSLayoutConstraint(item: collectionView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: 0.0))

    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    collectionView.addGestureRecognizer(panGesture)
    panGesture.isEnabled = false

    let tap = UILongPressGestureRecognizer(target: self, action: #selector(self.handleTap(sender:)))
    tap.delegate = self
    collectionView.addGestureRecognizer(tap)

}

func handlePan(sender: UIPanGestureRecognizer? = nil) {
    var locationOfBeganTap: CGPoint?

    if sender?.state == .possible { 
    }

    if sender?.state == .ended {
        startTime = NSDate.timeIntervalSinceReferenceDate
    }
}

func handleTap(sender: UIPanGestureRecognizer? = nil){            
    if sender?.state == .began {
        panGesture.isEnabled = true
        self.collectionView!.isScrollEnabled = false
    }

    if sender?.state == .ended {
    }
}

答案 3 :(得分:1)

在jsp中你可以这样做:

<%
boolean checkboxDisabled = true; //do your logic here
String checkboxState = checkboxDisabled ? "disabled" : "";
%>
<input type="checkbox" <%=checkboxState%>>

答案 4 :(得分:-1)

根据W3Schools,您可以使用JavaScript for disabled复选框。

<!-- Checkbox who determine if the other checkbox must be disabled -->
<input type="checkbox" id="checkboxDetermine">
<!-- The other checkbox conditionned by the first checkbox -->
<input type="checkbox" id="checkboxConditioned">
<!-- JS Script -->
<script type="text/javascript">
    // Get your checkbox who determine the condition
    var determine = document.getElementById("checkboxDetermine");
    // Make a function who disabled or enabled your conditioned checkbox
    var disableCheckboxConditioned = function () {
        if(determine.checked) {
            document.getElementById("checkboxConditioned").disabled = true;
        }
        else {
            document.getElementById("checkboxConditioned").disabled = false;
        }
    }
    // On click active your function
    determine.onclick = disableCheckboxConditioned;
    disableCheckboxConditioned();
</script>

您可以在此处看到演示文稿:http://jsfiddle.net/antoinesubit/vptk0nh6/

答案 5 :(得分:-2)

我知道这个问题有点旧,但这是我的解决方案。

 // HTML
 // <input type="checkbox" onClick="setcb1()"/>
 // <input type="checkbox" onClick="setcb2()"/>

 // cb1 = checkbox 1
 // cb2 = checkbox 2
  var cb1 = getId('cb1'), 
      cb2 = getId('cb2');

  function getId(id) {
    return document.getElementById(id);
  }

  function setcb1() {
    if(cb1.checked === true) {
      cb2.checked = false;
      cb2.disabled = "disabled"; // You have to disable the unselected checkbox
    } else if(cb1.checked === false) {
      cb2.disabled = ""; // Then enable it if there isn't one selected.  
    }
  }

  function setcb2() {
    if(cb2.checked === true) {
      cb1.checked = false;
      cb1.disabled = "disabled"
    } else if(cb2.checked === false) {
      cb1.disabled = ""
  }

希望这有帮助!