单击按钮时的文本框验证

shi*_*ima 0 html jquery

我正在制作一个文本框和按钮的简单网站.虽然点击按钮错误应该给出"文本框值不能为空".我试过很多网站,但我的问题还没有解决.一个重要的事情是提到这个按钮和文本框没有包含在表单中.

<input id="AccountText" type="text" name="AccountText" 
       class="form-control" placeholder="Name" required=''/>

<input id="AddAccount" type="button" class="btn btn-primary" value="Save"  />
Run Code Online (Sandbox Code Playgroud)

我已经尝试过controltovalidation但它也不适用于我.

Chr*_*ris 5

试试这个.应该帮助你开始.

HTML

<input id="AccountText" type="text" name="AccountText" value="" class="form-control" placeholder="Name" required=''/>

//submit button
<input class="button" type="submit" value="submit">
Run Code Online (Sandbox Code Playgroud)

JQUERY

基础知识:当用户单击提交按钮(使用该.click()功能)时,jquery会检查输入值是否为空(.val()用于获取输入值.)如果为空,则会显示一条消息(使用alert()),提示用户完成领域

$(document).ready(function(){
   $('.button').click(function(){
        if ($('#AccountText').val() == ""){
            alert('Please complete the field');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/ggChris/mfbaxkfp/