【问题标题】:if not checked input then follow如果未检查输入,则按照
【发布时间】:2011-03-28 14:37:33
【问题描述】:

我试图用谷歌搜索,但找不到任何关于这种情况的信息,我用谷歌搜索不好 :) 无论如何,我只需要创建一个带有一个输入复选框的表单,当复选框被选中时,它会转到一个链接,当它没有被选中时,它会跟随另一个链接,现在我卡在这里:

<form name="eol" method="post" 
                 action="/index.cfm?fuseaction=objects2.view_product_list&eol=1">

Show EOL Products<input type="checkbox" name="eol_input" value="0" <cfif not isdefined('attributes.eol')><cfelse>checked</cfif> onClick="eol.submit();">
</form>

当未选中复选框时,如何使其跟随其他链接?!我认为它可以通过 javascript(jquery) 来完成,所以有人知道吗?

感谢您的帮助!

【问题讨论】:

    标签: javascript jquery forms checkbox


    【解决方案1】:
    $("[name='eol_input']").click(function(){
        if(this.checked){
            //is checked
            window.location = "http://link1.com/";
        } else {
            //not checked
            window.location = "http://link2.com/";
        }
    });
    

    【讨论】:

    • @abrabr:出于对所有理智的热爱,请不要使用.is(":checked") 来测试单个复选框元素的状态。 this.checked 将在所有浏览器中为您提供相同的结果,并且性能更好。欲了解更多信息,请查看whattheheadsaid.com/2010/10/…。
    • 哇,这是一些速度差异...不错的文章,+1。好的,我会修改它。
    【解决方案2】:

    这是你所追求的吗?

    $(document).ready(function() {
        $('form[name="eol"]').submit(function(event) {
            event.preventDefault();
            event.stopPropagation();
    
            var urls = {
                "checked" : "http://foo.com/",
                "unchecked": "http://bar.com/"
            };
    
            var isChecked = $('input[name="eol_input"]:checked').length;
            window.location = isChecked ? urls['checked'] : urls['unchecked'];
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多