Javascript 基于radiobutton的jquery启用/禁用文本框故障排除
关于如何使用jquery启用基于单选按钮选择的文本字段,有很多文章。资料来源: 我对javascript没有任何经验,而且我无法让这些示例中的任何一个正常工作 我尝试了通过JFIDLE提供的多个示例,但它们在我的应用程序上不起作用 因此,一份清单: 1) javascript通常可以工作,因为我复制并粘贴了一个日期选择器小部件,它工作得很好。 2) 我有我的标签包装代码片段 3) 无论是在身体还是头部都没有区别 下面是我正在使用的示例代码 在视图中(这是codeigniter)Javascript 基于radiobutton的jquery启用/禁用文本框故障排除,javascript,jquery,html,Javascript,Jquery,Html,关于如何使用jquery启用基于单选按钮选择的文本字段,有很多文章。资料来源: 我对javascript没有任何经验,而且我无法让这些示例中的任何一个正常工作 我尝试了通过JFIDLE提供的多个示例,但它们在我的应用程序上不起作用 因此,一份清单: 1) javascript通常可以工作,因为我复制并粘贴了一个日期选择器小部件,它工作得很好。 2) 我有我的标签包装代码片段 3) 无论是在身体还是头部都没有区别 下面是我正在使用的示例代码 在视图中(这是codeigniter) //如果选择单
//如果选择单选按钮,则启用方框
$('input[name=“radioknof”]”)。在('click',function()上{
$(this).next().prop('disabled',false).同胞('input[type=text]')).prop('disabled',true);
});
很明显,我遗漏了一些简单的东西,比如语法错误。。。但我一辈子都看不到它。我不明白的是为什么jfiddle可以100%工作,而在我的本地服务器上却不能
(无论单选按钮选择如何,这两个框都将被禁用)
禁用的HTML表单输入属性不带参数。您可以设置它,也可以不设置它,因此,为了修复代码,当您要启用该字段时,请完全删除禁用属性。您有
,其中包含代码。你应该有一些更像:
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
// put your code in here
</script>
</body>
</html>
//把你的代码放在这里
或者在
中使用加载
事件。您不能定义src
,也不能将代码放入
标记中。我个人也会使用另一个外部的src
作为您的其他标记,因此它会被缓存到客户端的浏览器内存中。解决了这个问题。。。。正如我提到的,我没有任何经验,所以我只是回到jfiddle示例,查看底层代码,发现额外的行$(window.load)(function(){
,带结束符})代码>在末尾
完成下面的代码
<html>
<body>
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'> </script>
<script type='text/javascript'>
$(window).load(function(){
$('input[type=text]').prop('disabled',true);
$('input[name=radioknof]').on('click', function()
{
$(this).next().prop('disabled',true).siblings('input[type=text]').prop('disabled',false);
}
);
});
</script>
<input type="radio" name="radioknof" value="text1" />
<input type="text" id="id1" disabled="true"/><br/><br/>
<input type="radio" name="radioknof" value="text2"/>
<input type="text" id="id2"/>
</body>
$(窗口)。加载(函数(){
$('input[type=text]')。prop('disabled',true);
$('input[name=radioknof]')。在('click',function()上
{
$(this).next().prop('disabled',true).同胞('input[type=text]')).prop('disabled',false);
}
);
});
没有。没有什么变化。我明白了标记的意义,当我让它工作时,我将使用另一个外部源。但到目前为止没有任何改善。两个框都保持禁用状态。请尝试使用$(“#id”)
选择器,看看会发生什么。抱歉,我对javascript不够熟悉,无法理解这一点。您指的是.prop('disabled',true)代码>我猜?不太清楚你为什么说表单。。。本例中没有一个…?
标记是表单元素,这就是为什么我将它们称为表单元素。很高兴看到你在下面找到了答案!
<html>
<body>
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'> </script>
<script type='text/javascript'>
$(window).load(function(){
$('input[type=text]').prop('disabled',true);
$('input[name=radioknof]').on('click', function()
{
$(this).next().prop('disabled',true).siblings('input[type=text]').prop('disabled',false);
}
);
});
</script>
<input type="radio" name="radioknof" value="text1" />
<input type="text" id="id1" disabled="true"/><br/><br/>
<input type="radio" name="radioknof" value="text2"/>
<input type="text" id="id2"/>
</body>