简体   繁体   English

如何使用.. jquery和vb.net创建切换按钮?

[英]how to create a toggle button using .. jquery and vb.net?

I want this type of toggle button please click the link below to see my requirement : 我想要这种类型的切换按钮,请点击下面的链接以查看我的要求:

http://www.redbus.in/Booking/SeatSelection.aspx?rt=4017230&doj=30-Nov-2010&dep=04:55%20PM&showSpInst=false http://www.redbus.in/Booking/SeatSelection.aspx?rt=4017230&doj=30-Nov-2010&dep=04:55%20PM&showSpInst=false

You can use PrettyCheckBox 您可以使用PrettyCheckBox

or you can build it yourself with a bit of JS (better if you use jQuery) 或者您也可以用一些JS自己构建它(最好是使用jQuery)

Steps: with asp.net render all the checkbox you need and specify an ID (different for each) and a name for each, set also class. 步骤:使用asp.net渲染所有需要的复选框,并指定ID(每个ID都不同)和名称,还设置类。 Render also a LABEL with "for" attribute correctly set. 渲染一个带有“ for”属性的标签。 in the label put what should be your checkbox (images/links etc) 在标签中放入应该是您的复选框(图像/链接等)

  • for the check class, set css display:none 对于检查类,设置css display:none
  • with jquery bind the click event of each label, in the event raise the CLICK of the related checkbox (so it works also in IE), get the status of the checkbox and update the attributes (class is the best) of the content of the label. 使用jquery绑定每个标签的click事件,在事件中引发相关复选框的CLICK(因此它也适用于IE),获取复选框的状态并更新内容的属性(类是最好的)标签。

in code is: 在代码中是:

CSS: CSS:

.seatcheckbox{
 display:block;
 height:16px;
 width:16px;
 }
.checked {
    background-image: url(chair-red.png)
 }
.unchecked {
    background-image: url(chair-gray.png)
 }
input.hidden{ display:none;}

HTML rendered by asp.net 由ASP.NET呈现的HTML

<input type="checkbox" id="chk1" name="Seats" value="1" class="hidden" />
<label for="chk1"><div class="seatcheckbox"></div></label> 

JQERY script in the same html rendered by asp.net 由ASP.NET呈现的同一HTML中的JQERY脚本

<script type="text/javascript">

$(document).ready(function(){
  $('label').each(function (index, object) {
        if ($(this).attr("for") != "") {

            var ctl = $("#" + $(this).attr("for"));

            if (!ctl.is("input[type=radio]") && !ctl.is("input[type=checkbox]")) {
                return;
            }

            ctl.css("display", "none")

            $(this).click(function (e) {

                try {
                    if ($.browser.msie) {
                        ctl.click();
                    }
                    var lbl = $(this);
                    setTimeout(function () { adg_ChecksRefresh(lbl); }, 1)
                } catch (ex) { }

            });
});

function adg_ChecksRefresh(lbl) {
    var ctl = $("#" + $(lbl).attr("for"));
    var name = ctl.attr("name");
    var id= ctl.attr("id");
    var checked = ctl.is(":checked");

    if (checked) {
        $(lbl).removeClass("unchecked");
        $(lbl).addClass("checked");
    } else {
        $(lbl).removeClass("checked");
        $(lbl).addClass("unchecked");
    }
}

</script>

Don't forget to include this in your HEAD tag: 别忘了在您的HEAD标记中包含以下内容:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>

If you intend to use it also for RADIO BUTTON replace the entire function adg_ChecksRefresh(lbl) with this code: 如果打算将其也用于RADIO BUTTON,请使用以下代码替换整个函数adg_ChecksRefresh(lbl):

function adg_ChecksRefresh(lbl) {
var ctl = $("#" + $(lbl).attr("for"));
var name = ctl.attr("name");
var id= ctl.attr("id");
var checked = ctl.is(":checked");

if (ctl.is("input[type=radio]")) {
    $("input[name=" + name + "]").each(function () {
        $("label[for=" + $(this).attr("id") + "]").removeClass("checked").addClass("unchecked");
    });

}

if (checked) {
    $(lbl).removeClass("unchecked");
    $(lbl).addClass("checked");
} else {
    $(lbl).removeClass("checked");
    $(lbl).addClass("unchecked");
}

} }

the main JS is tested to work with all primary browser. 主JS已通过测试,可与所有主浏览器一起使用。 but i wrote in live the html that have to be generated. 但我现场写了必须生成的html。 hope it works all! 希望一切正常!

Byes 再见

What particular aspect of the seat picker control do you need? 您需要座位选择器控制的哪个方面? Is it simply that you wish to alternate between 2 images based on the user clicking them? 您是否只是想根据用户单击图像在2张图像之间进行切换? If so then you can simply use the toggle function of jquery to alternate between 2 classes or else set the src of an image each click. 如果是这样,那么您可以简单地使用jquery的切换功能在2个类之间切换,或者每次单击都设置图像的src。

// Include jQuery in your page
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>

// Create a script block for the toggling
<script type="text/javascript" language="javascript">
$('#seat').click(function() {
  $(this).find('img').toggle();
});
</script>

// Create your markup for the images.
<div id='seat'>
<img id="empty" src="empty_seat.png" alt="" width="100" height="123" style='display:block;' />
<img id="occupied" src="occupied_seat.png" alt="" width="100" height="123" style='display:none;' />
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM