简体   繁体   English

如何在Internet Explorer中禁用复选框

[英]How do I keep a Checkbox disabled in Internet Explorer

I have an application that pairs a textbox with a checkbox. 我有一个将文本框和复选框配对的应用程序。 The user can check the checkbox, which auto-populates the textbox with a specific dollar amount. 用户可以选中复选框,该复选框将使用特定的美元金额自动填充文本框。 If they uncheck the checkbox, this sets the textbox's value to zero. 如果他们取消选中该复选框,则会将文本框的值设置为零。 They can also enter a dollar amount in the textbox and an onblur event handler toggles the checkbox. 他们还可以在文本框中输入美元金额,并且onblur事件处理程序可以切换复选框。

The problem comes when they enter a dollar amount in the textbox and then check the checkbox with a mouse click. 当他们在文本框中输入美元金额,然后用鼠标单击选中复选框时,就会出现问题。 This fires the onblur event, which automatically toggles the checkbox, then recognizes the mouse click, setting the dollar amount back to zero. 这会触发onb​​lur事件,该事件会自动切换复选框,然后识别鼠标单击,将美元金额设置回零。

My solution was to disable the checkbox on textbox focus, then enable the checkbox on textbox onblur event. 我的解决方案是禁用文本框焦点上的复选框,然后启用文本框onblur事件上的复选框。

This works well in Firefox and Chrome, but fails miserably in Internet Explorer. 这在Firefox和Chrome中效果很好,但在Internet Explorer中却失败了。 FF and Chrome ignore any mouse click on the checkbox when it is disabled. 禁用FF和Chrome时,忽略该复选框上的任何鼠标单击。 This means that the onblur event does not fire, when the user clicks on the disabled checkbox after entering a dollar amount in the textbox. 这意味着当用户在文本框中输入美元金额后单击禁用的复选框时,onblur事件不会触发。 The checkbox stays disabled. 该复选框保持禁用状态。 They have to click elsewhere on the page for it to be enabled. 他们必须单击页面上的其他位置才能启用它。

In Internet Explorer, the onblur event fires when the user clicks on the disabled checkbox, and the checkbox recognizes the click, right after it is checked with the onblur event handler, unchecking the checkbox, setting the textbox value back to zero. 在Internet Explorer中,当用户单击禁用的复选框时,会触发onb​​lur事件,并且复选框在通过onblur事件处理程序进行检查后立即取消选中该复选框,并将文本框的值设置为零,从而识别了该单击。

I need a better solution. 我需要一个更好的解决方案。 How do I get Internet Explorer to act like FF and Chrome, ignoring any click on a disabled checkbox. 如何使Internet Explorer像FF和Chrome一样工作,而忽略对禁用复选框的任何单击。 Or, is there a more elegant solution altogether? 或者,是否有一个更优雅的解决方案?

Example Code: 示例代码:

<input type=textbox id=textbox1 onFocus=CheckboxDisable('pairedWithTextBox1'); onBlur=CheckboxEnable('pairedWithTextBox1');>
<input type=checkbox id=pairedWithTextBox1>

Javascript code: JavaScript代码:

function CheckboxDisable(id){
    document.getElementById(id).disabled = true;
}

function CheckboxEnable(id){
    document.getElementById(id).disabled = false;
}

Short of a real solution. 缺少真正的解决方案。 .. you COULD set a data attribute on the check box on focus of the text element, then check for it on the cb on click event and override the default action. ..你可以设置上的文本元素的焦点复选框中的数据属性,然后检查其上单击事件Cb和覆盖默认动作。 .. ..

Aside from a possibly confusing user interface design (can't say for sure since you genericized the problem too much), the problem is that the checkbox and textbox are both views of the same model . 除了可能引起混淆的用户界面设计(由于对问题的泛化程度过高,无法确定)之外,问题还在于复选框和文本框都是同一模型的 视图

  • The model is the dollar amount. 模型是美元金额。
  • The textbox is a view of the actual dollar amount. 文本框是实际美元金额的视图
  • The checkbox is a view that indicates whether the amount is 0 or something else. 复选框是一个视图 ,指示金额是0还是其他值。

Your current design is complex, which is not by itself a bad thing, because the event handlers for the text box onblur and checkbox onclick are also controllers of the model. 您当前的设计很复杂,这本身并不是一件坏事,因为文本框onblur和复选框onclick的事件处理程序也是模型的控制器 (This is a bit of an oversimplification; the controller also consists of the browser and all the JavaScript code.) (这有点过分简化;该控制器还由浏览器和所有JavaScript代码组成。)

Here is a solution that helps illustrate this fact. 这是一个有助于说明这一事实的解决方案。 It works based on the business rule that once the user has modified the value in the textbox (to a non-zero value) changing the state of the checkbox from unchecked to checked will not update the model (or the textbox view ). 它基于业务规则工作,一旦用户修改了文本框中的值(非零值),将复选框的状态从未选中更改为选中将不会更新模型(或文本框视图 )。

 var txtAmount = document.getElementById('txtAmount'); var chkAmount = document.getElementById('chkAmount'); var defaultValue = 0; var model = defaultValue; function UpdateViews() { if (model === 0) { chkAmount.checked = false; } txtAmount.value = model.toFixed(2); } function UpdateModel(val) { // update model when view changes model = parseFloat(val) || defaultValue; UpdateViews(); } UpdateViews(); // set initial view txtAmount.onchange = function () { UpdateModel(this.value); }; chkAmount.onclick = function () { if (this.checked) { // when user checks the box, only update model if not yet modified if (model === defaultValue) UpdateModel(55); // hardcoded default of $55 } else { UpdateModel(defaultValue); } }; 
 <input type='text' id='txtAmount' /> <input type='checkbox' id='chkAmount' /> 

If I were in your case, I will put an If-Else in the click event of the checkbox... 如果您愿意,我会在复选框的click事件中添加If-Else ...

Something like: 就像是:

        if (!String.IsNullOrEmpty(textBox1.Text) | textBox1.Text != "0")
        {
            // Do nothing since textbox1 already has a value greater than zero
        }
        else
        {
            // Enter amount in textbox
        }

Try this.. 尝试这个..

function CheckboxDisable(id){

$("#id").attr("disabled", "disabled");
}

function CheckboxEnable(id){
    $("#id").removeAttr("disabled");
}

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

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