【问题标题】:Disable h:inputText with jQuery使用 jQuery 禁用 h:inputText
【发布时间】:2014-03-26 18:29:28
【问题描述】:

我正在尝试通过 jQuery 使 h:inputText 禁用状态动态(由于性能原因不使用 reRender)。

我有一个 javascript 函数,它负责更新一堆 h:inputText 的值和其他状态。更新它们的值没有问题,但是在更新禁用状态时会发生奇怪的事情。我实际上无法禁用某个字段,即使尝试了以下所有选项:

jQuery(document.getElementById('form:fieldID')).attr('disabled', 'disabled');  

jQuery(document.getElementById('form:fieldID')).attr('disabled', true);

jQuery(document.getElementById('form:fieldID')).prop('disabled', true);  

在字段上调用上述任何选项,将导致最终的 html 等于 <input disabled="">,这也将启用以前禁用的字段。

我已经用 JSF 1.2 和一堆 jQuery 版本对此进行了测试。

【问题讨论】:

  • 试试document.getElementById('formID:fieldID')。示例<h:inputText id="answerForSQ" label="#{msg.answerForSQ}" value="#{securityQuestionsForm.answerForSQ}" styleClass="#{not securityQuestionsForm.secAnswerValid ? 'error' : ''}" onkeypress="if(window.event.keyCode==13){ window.name='answerQuestionWindow'; document.getElementById('answerQuestionForm:answerQues').focus();}"> </h:inputText>
  • 哦,忘了说:我已经为元素使用了完整的限定 ID
  • 试试 jQuery('#fieldID').attr('disabled', 'disabled');希望这会有所帮助
  • @SrikanthGanji 这相当于我的示例中的第一个选项。
  • @Joao Mosquito 我测试了它“jQuery('#fieldID').attr('disabled', 'disabled');”在我的一个页面中,它就像一个魅力。你为什么不试试 jQuery('#form\\:fieldID').attr('disabled', 'disabled');你必须用'\\'转义特殊字符。如果你想启用它,那么 jQuery('#form\\:fieldID').removeAttr('disabled');希望这会有所帮助

标签: javascript jquery html jsf


【解决方案1】:

你可以使用这个:http://api.jquery.com/attribute-ends-with-selector/

$("[name$=':fieldID']").attr("disabled", true); //for normal input field
$("[name$=':fieldID']").attr("disabled", true); //for checkbox and radiobutton (disable all options)
$("[name$=':fieldID'][id$=':1']").attr("disabled", true); //for checkbox and radiobutton second option

【讨论】:

  • 我想你误解了我的问题。我已经能够使用 jQuery 选择我想要的元素,并且在浏览器中进行调试时,DOM 树就是我想要的样子。但是当浏览器显示元素时,我试图禁用的所有元素都带有一个空的“禁用”属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 2013-02-18
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
相关资源
最近更新 更多