【发布时间】:2017-02-02 15:00:56
【问题描述】:
我找到了下面的代码来启用和禁用基于选项按钮的文本框,但我想要超过 20 个字段。
我想为此写很多脚本,有没有简单的方法可以创建 20 个这样的字段
请任何人帮助我解决这个问题
$(window).load(function() {
$(function() {
window.invalidate_input = function() {
if ($('input[name=opt13]:checked').val() == "Yes")
$('#others').removeAttr('disabled');
else
$('#others').attr('disabled', 'disabled');
};
$("input[name=opt13]").change(invalidate_input);
invalidate_input();
});
}); //]]>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.js"></script>
<label>Did you study any school</label>  
<input type='radio' name='opt13' value='Yes' id="" />Yes
<input type='radio' name='opt13' value='No' id="" />Others</td>
<br />
<br />
<label>Enter MatexBranch:</label>
<input type="textbox" name="others" id="others" />
【问题讨论】:
-
首先这不是一个好方法。
window.load有$(function(){})。为什么... -
for或foreach或$.each或者如果您对变量进行参数化,则可以使用多种迭代变量的方法中的任何一种。如果这是一个带有验证和条件格式的相当长/复杂的表单,也许看看这个question about generating forms based off JSON or XML -
@haxxxton 最好使用更通用的代码并使用
this来查找要操作的元素。 -
@Rajesh,我对“字段LIKE this”的含义的误解,而不是相同的,除了
name.. 我认为他们想要一个更强大的表单构建提供特定于 html 结构的解决方案。
标签: javascript jquery