Показать сообщение отдельно
  #6 (permalink)  
Старый 24.07.2014, 22:43
Профессор
Отправить личное сообщение для Rise Посмотреть профиль Найти все сообщения от Rise
 
Регистрация: 07.11.2013
Сообщений: 4,662

beerdy, учись студент
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<div id="dynamicbody">
	<textarea id="ma1" class="mindcommentarrea" style="width:290px;height:15px;"></textarea><br>
	<button id="mc1" class="mindcomment" style="display:none;">Кнопка</button><br>
	<textarea id="ma2" class="mindcommentarrea" style="width:290px;height:15px;"></textarea><br>
	<button type="button" id="mc2" class="mindcomment" style="display:none;">Кнопка</button><br>
</div>

<script>
$('#dynamicbody').on('click focusout','.mindcomment',hrButton).on('focusin focusout','.mindcommentarrea',hrTextarea);

function getBlock(from,elm) { // from: 1(from hrButton) / 0(from hrTextarea), elm: this
	var id = elm.id.substring(2);
	var block = [];
		block[0] = (from) ? $(elm) : $('#mc' + id);
		block[1] = (from) ? $('#ma' + id) : $(elm);
	return block; // block: [button,textarea]
}
function changeBlock(actn,elms) { // actn: 1(show) / 0(hide), elms: [button,textarea]
	var d = (actn)?['block','120px','340px']:['none','15px','290px'];
	elms[0].css('display',d[0]);
	elms[1].css({'height':d[1],'width':d[2]});
}
function checkFocus(whom,elms) { // whom: 1(check button) / 0(check textarea), elms: [button,textarea]
	var elm = (whom) ? elms[0] : elms[1];
	setTimeout(function() {
		if (!elm.is(':focus')) changeBlock(0,elms);
	}, 10);
}
function doAjax(block) { // block: [button,textarea]
	var mind_data_tosrv = {};
	mind_data_tosrv['action'] = 'comment';
	$.ajax({
		type: 'POST',
		url: 'http://',
		contentType: 'application/json; charset=UTF-8',
		data: JSON.stringify(mind_data_tosrv),
		success: function(data){
			// ...
			changeBlock(0,block); // выполнится при удачном завершении запроса 
			// ...
		},
		error: function(){            // ДЛЯ ПРИМЕРА
			setTimeout(function() {   // эмуляция удачного завершения запроса
				changeBlock(0,block); // выполнится 
			}, 3000);                 // через 3 сек 
		}
	});
}
function hrButton(event) {
	var block = getBlock(1,this);
	switch (event.type) {
		case 'click':
			doAjax(block);
			break;
		case 'focusout':
			checkFocus(0,block);
	}
}
function hrTextarea(event) {
	var block = getBlock(0,this);
	switch (event.type) {
		case 'focusin':
			changeBlock(1,block);
			break;
		case 'focusout':
			checkFocus(1,block);
	}
}
</script>
Ответить с цитированием