简体   繁体   English

动态设置Javascript中的复选框

[英]Set a checkbox in Javascript dynamically

Can someone please tell me how to set a checkbox dynamically in Javascript? 有人可以告诉我如何在Javascript中动态设置复选框吗?

function displayResults(html){
    var v = html.split(",");
    document.getElementById('fsystemName').value = v[0];
    if (v[1] == "true"){
        document.getElementById('fUpdateActiveFlag').checked = true;
    }
}

So I am passing some values from my controller separated by commas. 因此,我从控制器传递了一些值,并以逗号分隔。 For the checkbox, when the value is true I want it to tick the box. 对于复选框,当值为true时,我希望它在复选框中打勾。

EDIT: When a value is changed from a dropdown box it calls this displayResults method as its return statement: 编辑:从下拉框中更改值时,它将调用此displayResults方法作为其返回语句:

$('#uINewsSystemList').change(function() {
    $.get("/live-application/SystemById", {systemid: $("#systemList").val()}, displayResults, "html");

I want it to update some other values such as textboxes and checkboxes. 我希望它更新其他一些值,例如文本框和复选框。 I can get the fsystemName to populate with the appropriate value but the fUpdateActiveFlag always stays unchecked. 我可以使用适当的值填充fsystemName ,但fUpdateActiveFlag始终保持未选中状态。

In your question, you posted a javascript example, not JSP. 在您的问题中,您发布了一个JavaScript示例,而不是JSP。

In JSP you can use a for loop to write the checkbox like this: 在JSP中,您可以使用for循环来编写如下复选框:

<% for (Element element : elementList) { %>
<input type="checkbox" name="<%=element.getName() %>" value="<%=element.getValue() %>" <%=element.getChecked() ? "checked" : "" %> />
<% } %>

Will result in: 将导致:

<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>

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

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