2011-11-21 23 views
-5

我試圖運行一段代碼只有當一個checkbox刷新時使用正確的值?jquery複選框上的刷新和唯一值查詢

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"); 
} 

任何想法爲什麼上述代碼不起作用?

+1

簡化您的代碼並添加所有重要的代碼(例如HTML)。並更具體地針對你的問題。 –

+0

順便說一句,你可以發佈你的代碼在jsfiddle.net更容易回答 – JMax

回答

0

你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; 
} 
+0

理查德,你是上帝!非常感謝你我已經堅持了3天!非常感謝這個網站的支持。我相信我會回來添加我的2便士:) –

2

.val()您使用此兩行前失蹤的對象。:

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

嘗試 -

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

如果您使用的是最新的jQuery attr的版本將不會返回一個布爾值。如果選中該複選框,它將返回「已檢查」,否則將返回undefinedprop函數應該給你一個布爾值。更多資訊 - http://api.jquery.com/prop/

+0

這就是IPR101,PHP是放入或取消選中頁面刷新:) –