jQuery 选中复选框时启用文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14766894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:53:41  来源:igfitidea点击:

enable a textbox when check a checkbox

jquery

提问by Ehsan

I try using a jquery to enable a textbox when checked a checkbox. my pagejquery:

我尝试使用 jquery 在选中复选框时启用文本框。 我的页面jquery:

<script>
$(document).ready(function(){
  $('#isNews').change(function(){
    $("#newsSource").prop("disabled",false);
});
});
</script>

html:

html:

<label class="checkbox">
    <input type="checkbox" id="isNews" name="isNews">If you want send news:
</label>
<label for="newsSource" class="ilable">news source</label>
<input id="newsSource" name="newsSource" class="input-xlarge" disabled="" type="text" placeholder="news source">

what's the problem?

有什么问题?

回答by Arun P Johny

Change to

改成

$('#isNews').change(function(){
   $("#newsSource").prop("disabled", !$(this).is(':checked'));
});

Demo: Fiddle

演示:小提琴

回答by Sang Suantak

Apart from the error related to non-inclusion of jQuery, change your code to this:

除了与不包含 jQuery 相关的错误,将您的代码更改为:

$('#isNews').change(function () {
    $("#newsSource").prop("disabled", !this.checked);
});

回答by Explosion Pills

I don't speak Arabic, but you're including jQuery afterthe rest of all of your scripts. You need to include jQuery first.

我不会说阿拉伯语,但是您在所有脚本的其余部分之后都包含了 jQuery 。您需要包括jQuery的第一

You're getting "Undefined variable $" in the console. Always pay attention to the console.

您在控制台中收到“未定义的变量 $”。始终注意控制台。

回答by bipen

the error you have there is

你有的错误是

$ is not defined

so you probabaly havn't loaded jquery.. :)

所以你可能还没有加载 jquery .. :)

add this in your <head>tag

将此添加到您的<head>标签中

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

回答by Ali Foroughi

Here is your correct code

这是您的正确代码

<script>
$(document).ready(function(){
  $('#isNews').click(function(){
    if($(this).is(":checked"))
       $("#newsSource").removeAttr("disabled");
    else
       $("#newsSource").atte("disabled" , "disabled");
});
});
</script>

回答by Vikash Pandey

I opened the link given by you. in your page source the problem is you are including jquery after the the jquery code. Try adding it before the document.ready code.

我打开了你给的链接。在您的页面源代码中,问题是您在 jquery 代码之后包含了 jquery。尝试在 document.ready 代码之前添加它。