繁体   English   中英

故障排除jquery启用/禁用基于radiobutton的文本框

[英]Trouble shooting jquery enable/disable text box based on radiobutton

关于如何使用jquery基于单选按钮选择启用文本字段有很多帖子。 来源: 使用单选按钮(jQuery)禁用/启用文本字段

我没有使用javascript的经验,我无法使用这些示例中的任何一个。

我尝试过通过jfiddle提供的多个示例,但它们不适用于我的应用程序。

所以,一个清单:1)javascript一般工作,因为我复制并粘贴了一个日期选择器小部件,工作得很好。 2)我的标签包裹着代码片段3)无论是在正文还是标题中都没有区别。

这是我在视图中使用的示例代码(这是codeigniter)

<script src="//code.jquery.com/jquery-1.9.1.js">
//enable a box if the radio button is selected

$('input[name="radioknof"]').on('click', function() {
$(this).next().prop('disabled',false).siblings('input[type=text]').prop('disabled',true);
});
</script>

<input type="radio" name="radioknof" value="text1"/>
<input type="text" id="id1" disabled="disabled"/><br/><br/>
<input type="radio" name="radioknof" value="text2"/>  
<input type="text" id="id2" disabled="disabled"/>

显然我错过了一些像语法错误一样简单的东西......但我不能为我的生活看到它。 我不明白为什么jfiddle 100%工作,而在我的本地服务器上却没有。

(无论选择单选按钮如何,都会禁用这些框)

你有<script src="//code.jquery.com/jquery-1.9.1.js"> ,里面有代码。 你应该有更多的东西:

  <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>

或者在<head>使用load事件。 您无法定义src并将代码放在<script>标记内。 我个人也会为你的其他标签使用另一个外部src ,因此它会被缓存到你的客户端的浏览器内存中。

工作了....正如我提到的我没有经验,所以我只是回到jfiddle示例并查看底层代码并找到额外的行$(window).load(function(){ with closing }); 在末尾。

完整的代码如下。

<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>

禁用的HTML表单输入属性不带参数。 您要么设置了它,要么没有设置,因此为了修复代码,当您要启用该字段时,完全删除已禁用的属性。

暂无
暂无

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

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