測試是否使用jQuery選中了核取方塊

2021-10-05 07:02:02 字數 1883 閱讀 5124

本文翻譯自:testing if a checkbox is checked with jquery

if the checkbox is checked, then i only need to get the value as 1;如果選中該核取方塊,那麼我只需要將值設為1;otherwise, i need to get it as 0. how do i do this using jquery?否則,我需要將其設為0.如何使用jquery執行此操作?

$("#ans").val()will always give me one right in this case:$("#ans").val()在這種情況下總是給我乙個權利:

參考:測試是否使用jquery選中了核取方塊

just use$(selector).is(':checked')只需使用$(selector).is(':checked')

it returns a boolean value.它返回乙個布林值。

i've found the same problem before, hope this solution can help you.我之前發現了同樣的問題,希望這個解決方案可以幫到你。first, add a custom attribute to your checkboxes:首先,在核取方塊中新增自定義屬性:

write a jquery extension to get value:編寫乙個jquery擴充套件來獲取值:

$.fn.realval = function() else 

};

use code to get check-box value:使用**獲取核取方塊值:

$('#ans').realval();
you can test here

你可以在這裡測試一下

function chkb(bool)

var statusnum=chkb($("#ans").is(':checked'));

statusnum will equal 1 if the checkbox is checked, and 0 if it is not.如果選中該核取方塊,statusnum將等於1,如果不是,則為0。

edit: you could add the dom to the function as well.

function chkb(el)

var statusnum=chkb($("#ans"));

try this試試這個

$('input:checkbox:checked').click(function())
here flag is true if checked otherwise false如果選中則返回false,則此標誌為true

var flag=$('#ans').attr('checked');
again this will make cheked再次,這將使cheked

$('#ans').attr('checked',true);
// use ternary operators

$("#ans").is(':checked') ? 1 : 0;

jQuery判斷是否選中

1.判斷check是否選中 doctype html html lang en head meta charset utf 8 title 判斷check是否被選中 title meta name viewport content width device width,initial scale 1...

Jquery 判斷 checked 是否選中

如果要判斷js中的 checked 是否選中,首先取決於你引用的 jquery 的版本。首先,我引用的是 jquery v1.11.2 的版本出現的效果如下 建議 選用 jquery 1.8.3.min.js 會比較穩定點 charset utf 8 title head type checkbox...

JQuery判斷CheckBox是否選中

function checkvalidity return true var checkusername checkusername is checked var checkoperatetime checkoperatetime is checked var checkdescriptioninc...