简体   繁体   English

ASP.NET MVC,在“文本框”旁边显示文本

[英]ASP.NET MVC, display text just next to the Text Box

How can I display a text just next to the Text Box using jQuery, like if Name is not entered in the Form, so after clicking the Submit Button it will show the Text just next to the Name Text Box and I don't want to use the validation for this, I am checking the Text Box value using simple jQuery coding..? 我如何使用jQuery在文本框旁边显示文本,例如是否未在表单中输入名称,因此在单击“提交”按钮后,它将在“名称文本框”旁边显示文本,而我不想使用这种验证,我检查使用简单的jQuery编码的文本框的值..? Here is my Code.. 这是我的代码。

$('#btnAddContact').click(function () {
        if ($("#name").val().length <= 0) {
             $("#name").val("");
             $("#name").focus();
});

I think you are trying to use for validation message display 我认为您正在尝试用于验证消息显示

  1. Then put the textbox in a div 然后将文本框放入div
  2. Add float:left to textbox in css float:left添加到CSS中的文本框
  3. Then write content in to div or you can create a label or span in to that div and apply css float:right to that span or lablel 然后将内容写入div中,或者您可以在该div中创建标签或跨度,然后将css float:right应用于该跨度或标签

Just place a hidden div next to the name and display it on your function: 只需在名称旁边放置一个隐藏的div并将其显示在您的函数上:

<div id="errorName" style="display:none">Name can't be empty</div>

And then the function: 然后函数:

$('#btnAddContact').click(function () {
    if ($("#name").val().length <= 0) {
         $("#errorName").fadeIn();
         $("#name").val("");
         $("#name").focus();
    }
    else
       $("errorName").fadeOut();
});

Will add a new div after the name if it do not exist. 如果name不存在,将在name后添加一个新的div。

$('#btnAddContact').click(function () {
  var msg = $('#name-message');
  if (msg.length == 0) {
      msg = $('<div id="name-message"></div>')
      $('#name').after(msg);
  }

  msg.html('Opps. Error!');   
});

you can easily do it like this 你可以很容易地做到这一点

$('#btnAddContact').click(function () {
        if ($("#name").val().length <= 0) {
             $("#name").val("");
             $("#name").after('<span>Please enter name</span>');
             $("#name").focus();
        }
});

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

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