[英]Change background color of input
I have a code here, and what I want to accomplish is a live checking that when a username exist, the background color of that text input will change. 我在这里有一个代码,我想完成的是一个实时检查,以确保当存在用户名时,该文本输入的背景色将改变。 I'm using slim framework by the way and here is what I've accomplished for now: 顺便说一下,我正在使用苗条的框架,这是我目前已经完成的工作:
html and jQuery: html和jQuery:
<input type="text" name="txt_un" id="txt_un" />
<script>
$("#txt_un").keyup(function (e) {
var username = $(this).val();
$.post('/check/username/availability', {'username':username}, function(data) {
$("#user-result").html(data); //this will dump the data received from the php but I just want it to change the color of the input
});
});
</script>
php route: php路线:
$app->get('/check/username/availability', function() use($app) {
$db = new db();
$db->setErrorCallbackFunction("myErrorHandler", "text");
$request = \Slim\Slim::getInstance()->request()->post();
$app = \Slim\Slim::getInstance();
$bind = array(
":un" => $request['txt_un']
);
$result = $db->select("accounts", "username = :un", $bind);
if(count($result) > 0) {
//change background color of txt_un
} else {
//change background color of txt_un
}
});
How can I accomplish it using my code? 如何使用我的代码来完成? Or is there another way of doing this? 还是有另一种方法? Thank you. 谢谢。
You can output color code (#ffffff or red) or some flag in your php script. 您可以在PHP脚本中输出颜色代码(#ffffff或红色)或某些标志。 In JS you can use $(selector).css method. 在JS中,您可以使用$(selector).css方法。
function Change(color){
$('.example').css({
background: color
});
}
Change('red');
Try: http://jsfiddle.net/x7xm7svr/ 试试: http : //jsfiddle.net/x7xm7svr/
About method: http://api.jquery.com/css/ 关于方法: http : //api.jquery.com/css/
In your case, if the server method return 0 on already exist and 1 on not exist: 在您的情况下,如果服务器方法已存在则返回0,而不存在则返回1:
$("#txt_41").keyup(function (e) {
var username = $(this).val();
$.post('/check/username/availability', {'username':username}, function(data) {
if(data=='0')
{ $("#txt_un").css({background: 'red'}); }
else
{ $("#txt_un").css({background: 'green'}); }
});
});
I got it working using Roman Kirillov's answer and by changing the route from post to get. 我使用Roman Kirillov的答案并通过更改从邮递到获取的路线来使其工作。 Here is the final code: 这是最终代码:
Route: 路线:
$app->post('/check/username/availability', function() use($app) {
$db = new db();
$db->setErrorCallbackFunction("myErrorHandler", "text");
$request = \Slim\Slim::getInstance()->request()->post();
$app = \Slim\Slim::getInstance();
$bind = array(
":un" => $request['txt_un']
);
$result = $db->select("accounts", "username = :un", $bind);
if(count($result) > 0) {
echo 1;
} else {
echo 2;
}
});
jQuery: jQuery的:
$("#txt_41").keyup(function (e) {
var username = $(this).val();
$.post('/check/username/availability', {'username':username}, function(data) {
if(data=='0')
{ $("#txt_41").css({background: 'red'}); }
else
{ $("#txt_41").css({background: 'green'}); }
});
});
Thanks for viewing :) 感谢您的观看:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.