Tag Archive: jQuery


jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关
获取一组radio被选中项的值
var item = $(‘input[@name=items][@checked]‘).val();
获取select被选中项的文本
var item = $(“select[@name=items] option[@selected]“).text();
select下拉框的第二个元素为当前选中值
$(‘#select_id’)[0].selectedIndex = 1;
radio单选组的第二个元素为当前选中值
$(‘input[@name=items]‘).get(1).checked = true;

获取值:

文本框,文本区域:$(“#txt”).attr(“value”);
多选框checkbox:$(“#checkbox_id”).attr(“value”);
单选组radio: $(“input[@type=radio][@checked]“).val();
下拉框select: $(‘#sel’).val();

控制表单元素:
文本框,文本区域:$(“#txt”).attr(“value”,”);//清空内容
$(“#txt”).attr(“value”,’11′);//填充内容

多选框checkbox: $(“#chk1″).attr(“checked”,”);//不打勾
$(“#chk2″).attr(“checked”,true);//打勾
if($(“#chk1″).attr(‘checked’)==undefined) //判断是否已经打勾

单选组radio: $(“input[@type=radio]“).attr(“checked”,’2′);//设置value=2的项目为当前选中项
下拉框select: $(“#sel”).attr(“value”,’-sel3′);//设置value=-sel3的项目为当前选中项
$(“

“).appendTo(“#sel”)//添加下拉框的option
$(“#sel”).empty();//清空下拉框

jquery 在scuess 中访问全局变量

如何在下面的代码的 callback 之中访问全局变量

Js代码

function test(){
$.ajax({
url: “test.txt”, cache: false, dataType:”json”,
//data:’id=’+$(“#id”).value(),
success: function(json){

g_aaa = json.name;
}
});
}
$(document).ready(function (){
var g_aaa = “”;
alert(g_aaa);

});

解决代码如下: url: “test.txt”, async: false, cache: false, dataType:”json”,

Simple jQuery Modal Window Tutorial

Demonstration Download

Introduction

In this tutorial, I’m going to share how to create a simple modal window with jQuery. I like jQuery, it makes everything so simple and so easy. In case you don’t know what’s modal window. You can click here. That’s an example of a modal window.

In this website, I’m using facebox (inspiration from facebook). Others, such as lightbox, thickbox, multibox, litebox…… it’s too many of them and they all are having different features.

Right, let’s start, this example will show you how to create a modal window that will display the content of a DIV #ID.

My objectives are:

  • Able to search the whole html document for A tag NAME=”modal” attribute, so when users click on it, it will display the content of DIV #ID in the HREF attribute in Modal Window.
  • A mask that will fill the whole screen.
  • Modal windows that is simple and easy to modify.

1. HTML code and A tag attributes

  1. <!– #dialog is the id of a DIV defined in the code below –>
  2. <a href=”#dialog” name=”modal”>Simple Modal Window</a>
  3. <div id=”boxes”>
  4. <!– #customize your modal window here –>
  5. <div id=”dialog” class=”window”>
  6. <b>Testing of Modal Window</b> |
  7. <!– close button is defined as close class –>
  8. <a href=”#” class=”close”>Close it</a>
  9. </div>
  10. <!– Do not remove div#mask, because you’ll need it to fill the whole screen –>
  11. <div id=”mask”></div>
  12. </div>
<!-- #dialog is the id of a DIV defined in the code below -->
<a href="#dialog" name="modal">Simple Modal Window</a>

<div id="boxes">

	<!-- #customize your modal window here -->

	<div id="dialog">
		<b>Testing of Modal Window</b> | 

		<!-- close button is defined as close class -->
		<a href="#">Close it</a>

	</div>

	<!-- Do not remove div#mask, because you'll need it to fill the whole screen -->
 	<div id="mask"></div>
</div>

2. CSS code

  1. <style>
  2. /* Z-index of #mask must lower than #boxes .window */
  3. #mask {
  4. position:absolute;
  5. z-index:9000;
  6. background-color:#000;
  7. display:none;
  8. }
  9. #boxes .window {
  10. position:absolute;
  11. width:440px;
  12. height:200px;
  13. display:none;
  14. z-index:9999;
  15. padding:20px;
  16. }
  17. /* Customize your modal window here, you can add background image too */
  18. #boxes #dialog {
  19. width:375px;
  20. height:203px;
  21. }
  22. </style>
<style>

/* Z-index of #mask must lower than #boxes .window */
#mask {
  position:absolute;
  z-index:9000;
  background-color:#000;
  display:none;
}

#boxes .window {
  position:absolute;
  width:440px;
  height:200px;
  display:none;
  z-index:9999;
  padding:20px;
}

/* Customize your modal window here, you can add background image too */
#boxes #dialog {
  width:375px;
  height:203px;
}
</style>

3. Javascript

  1. <script>
  2. $(document).ready(function() {
  3. //select all the a tag with name equal to modal
  4. $(‘a[name=modal]‘).click(function(e) {
  5. //Cancel the link behavior
  6. e.preventDefault();
  7. //Get the A tag
  8. var id = $(this).attr(‘href’);
  9. //Get the screen height and width
  10. var maskHeight = $(document).height();
  11. var maskWidth = $(window).width();
  12. //Set height and width to mask to fill up the whole screen
  13. $(‘#mask’).css({‘width’:maskWidth,’height’:maskHeight});
  14. //transition effect
  15. $(‘#mask’).fadeIn(1000);
  16. $(‘#mask’).fadeTo(“slow”,0.8);
  17. //Get the window height and width
  18. var winH = $(window).height();
  19. var winW = $(window).width();
  20. //Set the popup window to center
  21. $(id).css(‘top’,  winH/2-$(id).height()/2);
  22. $(id).css(‘left’, winW/2-$(id).width()/2);
  23. //transition effect
  24. $(id).fadeIn(2000);
  25. });
  26. //if close button is clicked
  27. $(‘.window .close’).click(function (e) {
  28. //Cancel the link behavior
  29. e.preventDefault();
  30. $(‘#mask, .window’).hide();
  31. });
  32. //if mask is clicked
  33. $(‘#mask’).click(function () {
  34. $(this).hide();
  35. $(‘.window’).hide();
  36. });
  37. });
  38. </script>
<script>

$(document).ready(function() {	

	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');

		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();

		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		//transition effect
		$('#mask').fadeIn(1000);
		$('#mask').fadeTo("slow",0.8);	

		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);

		//transition effect
		$(id).fadeIn(2000); 

	});

	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').hide();
	});		

	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			

});

</script>

It’s very straight forward and easy to understand. Remember, you need to include jQuery framework.

Demonstration Download

4. Launch modal window with Javascript

Due to popular demand :) , I have an example for it. The concept is simple. I wrapped the modal window script inside a function, and then you will able to call the modal window using javascript function call.

Yes, you will able to load the modal window on page load as well :)

  1. $(document).ready(function () {
  2. //id is the ID for the DIV you want to display it as modal window
  3. launchWindow(id);
  4. });
$(document).ready(function () {
  //id is the ID for the DIV you want to display it as modal window
  launchWindow(id);
});

Launch Modal Window with Javascript

And, if you want to close the modal window on key press, any keys you want, you can add the following function.

  1. $(document).keyup(function(e) {
  2. if(e.keyCode == 13) {
  3. $(‘#mask’).hide();
  4. $(‘.window’).hide();
  5. }
  6. });
$(document).keyup(function(e) {
  if(e.keyCode == 13) {
    $('#mask').hide();
    $('.window').hide();
  }
});

I think I should make another post about modal window. :)

5. Conclusion

Yes, that’s all you need to make a simple jquery modal window. In this tutorial, it shown you the concept of how to display DIV content inside a modal window. However, you can further develop it to accept a link and display it in an iFrame and image gallery.

For those who’s looking for a fully customizable Modal Window, you can try my method, if you have any other questions, please let me know. Thanks for reading.

Update

22-5-2009: – Added a new section “Activate modal window with Javascript”

16-4-2009: – If you prefer this article in Portuguese, please visit Simple jQuery Modal Window in Portuguese by Maujor

27 Mar 09: – Added e.preventDefault() to link to launch the modal window.
- Changed css position to fixed, so that the modal window also fixed to center. – Changed var winH = $(window).height(); to var winH = $(window).height();

24 Mar 09:
- Added e.preventDefault() to cancel the anchor link effect, we can also able to cancel it by removing the href attribute.
- Changed var winH = $(window).height(); to var winH = $(document).height();

Powered by WordPress | Theme: Motion by 85ideas.