jquery在刷新和唯一值查询上选中复选框

Posted

技术标签:

【中文标题】jquery在刷新和唯一值查询上选中复选框【英文标题】:jquery checked checkbox on refresh and unique value query 【发布时间】:2012-01-02 23:52:42 【问题描述】:

只有在使用正确值刷新时检查checkbox 时,我才尝试运行一段代码?

if ($("input[name='business_group']").attr("checked")==true && .val("accommodation"))  
    $(".acc_title").show();
    $(".fd_title").hide();
    $(".dest_title").hide();
    $(".ttt_title").hide();
    $(".c_name_container").show();
    $(".stay").show();
    $(".visit").hide();

    alert("accommodation checked"); 
 
else if ($("input[name='business_group']").attr("checked")==true && .val("food_drink"))    
    $(".fd_title").show();
    $(".acc_title").hide();
    $(".dest_title").hide();
    $(".ttt_title").hide();
    $(".c_name_container").show();
    $(".stay").hide();
    $(".visit").show();

    alert("food_drink checked");

知道为什么上面的代码不起作用吗?

【问题讨论】:

简化您的代码并添加所有重要的代码(例如 html)。并更具体地说明您的问题。 顺便说一句,您可以在 jsfiddle.net 上发布您的代码以便于回答 【参考方案1】:

您在使用 this 的两行中都缺少.val() 之前的对象。:

if ($("input[name='business_group']").attr("checked")==true && .val("accommodation"))  

【讨论】:

【参考方案2】:

你的jQuery代码有点错误,这应该是你想要的:

if ($("input[name='business_group']:checked").val() === "accommodation")     
    // code here
 else if ($("input[name='business_group']:checked").val() === "food_drink") 
    // code here

事实上,最好将此值保存到变量中:

var checkedValue = $("input[name='business_group']:checked").val();
switch(checkedValue) 
    case "accommodation": // do stuff; break;
    case "food_drink": // do other stuff; break;

【讨论】:

理查德你是神!!!非常感谢,我已经坚持了3天了!!非常感谢这个网站的支持。我确定我会回来加我的 2 便士 :)【参考方案3】:

试试-

if ($("input[name='business_group']").prop("checked")==true && $("input[name='business_group']").val("accommodation")) 

如果您使用的是最新版本的 jQuery,attr 将不会返回布尔值。如果复选框被选中,它将返回“已选中”,如果未选中,它将返回 undefinedprop 函数应该返回一个布尔值。更多信息在这里 - http://api.jquery.com/prop/

【讨论】:

这是ipr101,php在刷新时将选中或未选中进入页面:)

以上是关于jquery在刷新和唯一值查询上选中复选框的主要内容,如果未能解决你的问题,请参考以下文章

jquery无刷新取值问题~~~~,很急~~~在线等!!

Kendo Grid:列标题复选框“选中所有”,在网格所有页面上选中复选框

jQuery - 根据 attr 值仅获取选中复选框的唯一值

在先前选择的复选框的基础上选择复选框

使用 jquery 表中的复选框从数组中删除 JavaScript 对象

使用 jquery ajax 在会话或会话存储中存储值