javascript 我如何使输入字段和提交按钮变灰
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3746115/
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
How do i grey out the input field and the submit button
提问by Matt Elhotiby
I want to grey out these two thingsthe song input field and the submit button until the user enters and artist. Is there an easy way to do this via JQuery.
我想将歌曲输入字段和提交按钮这两个东西变灰,直到用户输入和艺术家。有没有一种简单的方法可以通过 JQuery 做到这一点。
the id of the artist input field is #request_artist
艺术家输入字段的 id 是 #request_artist
回答by xil3
You can do something like this:
你可以这样做:
var artist = ('#request_artist');
var song = ('#request_song');
var assubmit = ('#request_submit');
song.attr('disabled', true);
assubmit.attr('disabled', true);
artist.change(function() {
if(artist.val() > 0) {
song.attr('disabled', false);
assubmit.attr('disabled', false);
} else {
song.attr('disabled', true);
assubmit.attr('disabled', true);
}
});
回答by ipsum
for the input field, the submit button should be equal $('#request_artist').attr('disabled', true);
对于输入字段,提交按钮应该等于 $('#request_artist').attr('disabled', true);
回答by Siva Gopal
The one liner code would be :
一个班轮代码是:
<input type="text" name="name" value="" id="txt1" />
<input type="button" name="name" id="btn1" disabled="disabled" value="Submit" />
<script type="text/javascript">
$("#txt1").keyup(function () {
$("#btn1").attr("disabled", $.trim($("#txt1").val()) != "" ? "" : "disabled");
});
</script>

