2014-02-07 31 views
0

我正在嘗試爲每個檢查/取消選中這些複選框的網站提出請求。 但是,這裏的問題在於我包含在我的網頁中的事件不起作用;當我檢查/取消選中沒有任何反應時。如何處理來自jQuery腳本的事件?

這是我的HTML代碼:

<form method="get" style="display:inline;" class="ListeAlertes"><input type="checkbox" name="fixee" value="true" style="margin-left:40px;margin-right:3px;" @ViewBag.Fixee /> Alertes fixées<input type="checkbox" name="nonFixee" value="true" style="margin-left:10px;margin-right:3px;" @ViewBag.NonFixee /> Alertes non-fixées</form> 

jQuery代碼:

$(".ListeAlertes input[type='checkbox']").change(function() { 
    var form = $(".ListeAlertes").serialize(); 
    if (($("input[name='fixee']:checked") && $("input[name='nonFixee']:checked")) || ($("input[name='fixee']:unchecked") && $("input[name='nonFixee']:unchecked"))) { 
     $.get("~/Alertes/Index?fixee=true&nonFixee=true"); 
    } 
    if ($("input[name='fixee']:checked") && $("input[name='nonFixee']:unchecked")) { 
     $.get("~/Alertes/Index?fixee=true&nonFixee=false"); 
    } 
    if ($("input[name='fixee']:unchecked") && $("input[name='nonFixee']:checked")) { 
     $.get("~/Alertes/Index?fixee=false&nonFixee=true"); 
    } 
}); 

任何高招嗎?

回答

0
$('#chkBoxId').click(function() { } 

您可以使用click()函數。

此處指定的'chkBoxId'是複選框的ID。

+0

我需要檢查,如果兩個複選框也選中或取消選中。你能不能更新我寫的內容來更好地理解你在這裏做什麼? – user3264174

+0

在你提供的小提琴中:[jsfiddle](http://jsfiddle.net/eKa8S)更正警報的拼寫。現在警報即將到來。接下來,你不能直接檢查「取消」。可以檢查'checked'的.attr()爲false。進行這些更改並且您的代碼將會正常。 – Jobin

+0

我試圖讓它更好的編碼,我有這個:http://jsfiddle.net/eKa8S/4/警告框還沒有出現。 – user3264174

0

嘗試點擊事件上$(「checekbox」), 也確保jQuery的CDN或者其文件包含在我們的代碼

+0

我需要檢查兩個複選框是否也被選中或未選中。你能不能更新我寫的內容來更好地理解你在這裏做什麼? – user3264174

+0

你可以使用$(「input:checked」).length來獲得總數。檢查過的元素,否則你可以使用:檢查單獨檢查它們。 – anam

+0

是不是我寫的jQuery腳本?但是,這裏的問題是如何使發佈的腳本一起工作? – user3264174

相關問題