如何在下面的文本字段中显示错误消息,下面显示的脚本和表单没有警告消息?
脚本
success: function (data)
{
scrollDown();
var message = $("#messagee").val();
if(message == "" || message == null){
alert("enter a message");
}
else {
$('#chat_log').append('<div class="row msg_container base_receive"><div class="col-md-12 col-xs-12"><div class="messages msg_receive"><p><a>' + message + '</a></p></div></div></div>');
$('#messagee').val('');
}
},
形式
<form class="form-horizontal msg_fixed_bottom send_message_form" id="data_form" method="POST" role="form" action="#" onsubmit="return validation();">
<div class="panel-footer" id="myForm" >
<div class="input-group submit_group">
<input id="messagee" name="messagee" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." value="<?php echo set_value('messagee'); ?>" />
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="submit" name="submit">Send</button>
</span>
</div>
答案 0 :(得分:1)
您可以在输入后添加隐藏的div,并仅在出现错误时显示。
html:
<div class="panel-footer" id="myForm" >
<div class="input-group submit_group">
<input id="messagee" name="messagee" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." value="<?php echo set_value('messagee'); ?>" />
<div id="errors" style="display:none;"></div>
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="submit" name="submit">Send</button>
</span>
</div>
js:
success: function (data)
{
scrollDown();
var message = $("#messagee").val();
if(message == "" || message == null){
$("#errors").html("enter a message");
$("#errors").fadeIn();
}
else {
$('#chat_log').append('<div class="row msg_container base_receive"><div class="col-md-12 col-xs-12"><div class="messages msg_receive"><p><a>' + message + '</a></p></div></div></div>');
$('#messagee').val('');
}
},
不要忘记在validation()函数开头隐藏和清空div(在 Ajax调用之前):
function validation() {
$("#errors").fadeOut();
$("#errors").html("");
$.ajax({
...
})
}
答案 1 :(得分:0)
形式
<form class="form-horizontal msg_fixed_bottom send_message_form" id="data_form" method="POST" role="form" action="#" onsubmit="return validation();">
<div class="panel-footer" id="myForm" >
<div class="input-group submit_group">
<input id="messagee" name="messagee" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." value="<?php echo set_value('messagee'); ?>" />
<span id='error_msg' class="yourcssclass"></span>
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="submit" name="submit">Send</button>
</span>
</div>
JS
success: function (data)
{
scrollDown();
var message = $("#messagee").val();
if(message == "" || message == null){
document.getElementById('error_msg').innerHTML='enter a message';
}
else {
$('#chat_log').append('<div class="row msg_container base_receive"><div class="col-md-12 col-xs-12"><div class="messages msg_receive"><p><a>' + message + '</a></p></div></div></div>');
$('#messagee').val('');
}
},
答案 2 :(得分:0)
用这个替换你的行:
<input type="text" name="uname" value="<?php echo set_value('uname'); ?>" /><div id="infoMessage"><?php echo form_error('uname'); ?></div></br>
更改变量名称,使其与变量名称匹配 阅读CI documentation了解更多信息。