I have a strange bug in my code. I have a view page that will display a dynamic list of items to be renamed. I have a view model that holds a few lists, with some id ints, the old name and the new name. I use a for loop in the view to create a label and a text box for each item. There is also a checkbox next to the text box that allows the user to chose not to rename that item. This checkbox has an onclick to toggle the disabled property of it's text box.
<%for (int i = 0; i < Model.ChargeIds.Count; i++)
{ %>
<div style="clear:left; float:left; margin-right:10px">
<label><%=Model.OldChargeNames[i]%></label>
<div class="editor-field">
<%=Html.TextBoxFor(m => m.NewChargeNames[i])%>
<%=Html.CheckBoxFor(m => m.DoNotCopyFlags[i], new { onclick = "toggleElement('NewChargeNames_" + i + "_')" })%>
<label>Don't Copy</label>
</div>
</div>
<%} %>
<script type="text/javascript">
function toggleElement(element) {
document.getElementById(element).disabled = !document.getElementById(element).;
};
</script>
Everything works nicely, until I click the checkbox next to the first item in the list, and then submit. My 'NewChargeNames' list comes back null. This does not happen if I disa开发者_JAVA百科ble any of the other text boxes, only the first one. I changed my toggle function to just re-style the textbox to look disabled, which works ok for now. But I have no idea why this bug happens, can anyone shed any light?
IE will not submit form values for disabled controls. I've encountered this situation many times and have two workarounds:
- re-enable disabled controls in the onsubmit event of the form.
- place the value for the disabled control in a hidden form field.
I don't think this is so much as a "bug", but a function of how IE handles form fields of various states.
精彩评论