반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

switch 관련 예제들

2012. 12. 8. 05:18 | Posted by 솔웅


반응형

Examples of manipulation of switches




switch 의 값을 서버로 전달하고 다른 윈도우를 display 합니다.

여기서 서버로 클릭된 스위치의 값을 전달할 겁니다. 그러면 서버는 그 값을 가지고 있는 새 window를 보내 줄 겁니다.


<!DOCTYPE html>
<html> 
<head> 
<meta name=viewport content="user-scalable=no,width=device-width" />
<link rel=stylesheet href=jquery.mobile/jquery.mobile.css />
<script src=jquery.js></script>
<script src=jquery.mobile/jquery.mobile.js></script>
</head> 

<body> 

<div data-role=page id=home>
<div data-role=header>
<h1>Home</h1>
</div>

<div data-role=content> 
<span> Would you like an apartment: </span>
<select data-role=slider>
<option value=no> No </option>
<option value=yes> Yes </option>
</select>
</div>
</div>

</body>
</html>

<script>

var value = $("select").val ();
$("select").bind ("change", function (event)
{
if (value == $(this).val ()) return;
value = $(this).val ();

var data = { };
data["value"] = value;

$.mobile.changePage ("action.php", { data : data } );
});

</script>




switch를 클릭할 때마다 서버를 call 해서 스위치의 값을 가지고 있는 새 window 를 보여줍니다. 초기 단계에 그 값이 이전것과 같은 것인지 아닌지를 체크합니다. 


action.php file


<?
$value = $_REQUEST["value"];
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv=Content-Type content=text/html;charset=iso-8859-1 />
</head>

<body>
<div data-role=page id=win2 data-add-back-btn=true>
<div data-role=header>
<h1>Window 2</h1>
</div>

<div data-role=content>
<p> Switch value: <?= $value ?> </p>
</div>
</div>
</body>

</html>


   

스위치를 클릭하면 아래와 같은 화면을 보실 겁니다.





action20.php

tistory518_01.html



정보를 전달할 때 submit button 사용하기

스위치를 클릭할 때마다 정보를 전달하는 것이 아니라 사용자가 Submit button 을 눌렀을 때 이 동작이 일어나도록 하겠습니다.

<!DOCTYPE html>
<html> 
<head> 
<meta name=viewport content="user-scalable=no,width=device-width" />
<link rel=stylesheet href=jquery.mobile/jquery.mobile.css />
<script src=jquery.js></script>
<script src=jquery.mobile/jquery.mobile.js></script>
</head> 

<body> 

<div data-role=page id=home>
<div data-role=header>
<h1>Home</h1>
</div>

<div data-role=content> 
<form action=action.php>
<span> Would you like an apartment: </span>
<select data-role=slider name=switch>
<option value=no> No </option>
<option value=yes> Yes </option>
</select>
<input type=submit value=OK>
</form>
</div>
</div>

</body>
</html>

<script>

</script>


 

OK 버튼을 누르면 스위치가 form 에 insert 됩니다. 그리고 action.php 파일을 call 하죠. 자바스크립트 부분은 없습니다. 모든 프로세스는 jQuery Mobile 에 의해서 진행됩니다.

이전 예제랑 비교해서 반드시 해야 할 게 있습니다. 바로  <select> element  에  name attribute 를 명시해아 하는 겁니다. 이 attribute와 그 값이 서버로 전달이 될 것이기 떄문이죠.

action.php file 


<?
$switch = $_REQUEST["switch"];
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv=Content-Type content=text/html;charset=iso-8859-1 />
</head>

<body>
<div data-role=page id=win2 data-add-back-btn=true>
<div data-role=header>
<h1>Window 2</h1>
</div>

<div data-role=content>
<p> Switch value: <?= $switch ?> </p>
</div>
</div>
</body>

</html>


action21.php

tistory518_02.html



반응형