jQuery禁用子节点
每当单选按钮设置为“否”选项时,我试图禁用web表单特定部分中的每个输入字段(并在选择“是”时还原此字段,但无法正常工作。我当前有此代码用于“监视”单选按钮并根据需要启用/禁用:jQuery禁用子节点,jquery,jquery-selectors,Jquery,Jquery Selectors,每当单选按钮设置为“否”选项时,我试图禁用web表单特定部分中的每个输入字段(并在选择“是”时还原此字段,但无法正常工作。我当前有此代码用于“监视”单选按钮并根据需要启用/禁用: jQuery("#ReportOn").change(function () { if(jQuery("#ReportOff").is(":checked")) { // Everything working up until here , but below command does noth
jQuery("#ReportOn").change(function () {
if(jQuery("#ReportOff").is(":checked")) {
// Everything working up until here , but below command does nothing.
jQuery("#reportform > input").attr("disabled", true);
} else if(jQuery("#ReportOn").is(":checked")) {
jQuery("#reportform > input").removeAttr("disabled");
}
});
表单如下所示:
<form method="post" action="post.php">
<input type="radio" name="report" id="ReportOn" value="1" /><label for="ReportOn">Yes</label>
<input type="radio" name="report" id="ReportOff" value="0" /><label for="ReportOff">No</label>
<div id="reportform">
<input type="text" name="title" id="ReportTitle" />
<input type="text" name="subtitle" id="ReportSubtitle" />
</div>
</form>
对
不
每当Reporting(报告)单选按钮设置为No(否)时,应禁用表单的其余部分
我在这里遗漏了什么使它无法工作?尝试更改:
jQuery("#reportform > input")
致:
也许你有一些错误,比如两张相同id的表格?
因为它对我有用,所以请看以下内容:这是针对同一任务的另一个解决方案
$('input[name="report"]').change(function(){
var disabled = ($(this).val() !== "1");
$('#reportform input').attr("disabled", disabled);
});
你的剧本似乎很管用 确保将其放在HTML中声明的两个DOM元素(ReportOn、ReportOff)之后。脚本不知道它们的存在 这将有助于:
<div id="something"></div>
<script>$("#something").text('hello');</script>
$(“#某物”).text('hello');
这个不会:
<script>$("#something").text('hello');</script>
<div id="something"></div>
$(“#某物”).text('hello');
为了避免此类问题,您应该在文档完全加载后运行脚本--document.ready()方法。控制台/firebug中是否有任何错误?Chrome控制台中仅显示此消息,但它看起来无害(信息性消息):(4)event.layerX和event.layerY在WebKit中已被破坏和弃用。它们将在不久的将来从引擎中删除。我自己的代码中没有任何event.layerX/layerY引用,也许它在jquery核心中。这不应该有什么区别,因为
输入
元素是#reportform
的直接子元素。也许是应该不会有什么不同,但它确实有。从选择器中删除>就可以了!谢谢。可能您的脚本正在运行时添加了一些内容。请尝试在浏览器控制台中运行jQuery(#reportform),以查看脚本运行时它是如何构造的。谢谢,这非常干净:)
<script>$("#something").text('hello');</script>
<div id="something"></div>