如果textarea为空,则禁用链接按钮 [英] Disabling link button if textarea is empty

查看:104
本文介绍了如果textarea为空,则禁用链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基本的按钮单击,可将文本附加到可拖动的div.为了触发按钮事件,必须将文本引入到textareafield中.这只是验证空字段的一种非常基本的方法.与按钮配合使用效果很好.现在的问题是我正在使用link button,但是我试图通过使用e.preventDefault()禁用它,但是它不起作用. JSFIDDLE

I have basic button click that appends text a draggable div. In order for the button event to trigger, text has to be introduced to the textareafield. This is just a very basic way to validate for empty fields. It works well with button. The issue is now I am using a link button but I am trying to disable it by using e.preventDefault() but it is not working. JSFIDDLE

$('.form-control').prop('disabled',true);
$('textarea').keyup(function (e){
        //$('#button').prop('disabled', this.value == "" ? true : false);
        $('#button').prop( this.value == "" ? e.preventDefault() : false);
});

HTML

<textarea rows="4" cols="50" placeholder="Enter Text Here!"></textarea>
<br/>
<!--input type="button" id="button" disabled="disabled" value="Add Div with Text" /-->
<a href="#" id="button" role="button"><i class="fa fa-plus"></i> Post Sticky</a>
<br/>
<div>
    <div class="middle-side empty"></div>

</div>

推荐答案

如果textarea值不正确(例如,为空),则可以简单地检查textarea值并防止#button单击处理程序上的行为.

You can simply check for textarea value and prevent behaviour on #button click handler if textarea value is incorrect (for example, is empty).

更新了小提琴

$('#button').click(function (e)
{
    if ($('textarea').val() == "")
    {
        return false;
    }
    ... other code

这篇关于如果textarea为空,则禁用链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆