当前位置: 代码迷 >> Web前端 >> jquery对于checkbox一些处置
  详细解决方案

jquery对于checkbox一些处置

热度:184   发布时间:2012-10-06 17:34:01.0
jquery对于checkbox一些处理

<input type="checkbox" name="foo" value="bar" />
<input type="button" onclick="show_checked()" value="Show" />
<input type="button" onclick="set_checked(true)" value="On" />
<input type="button" onclick="set_checked(false)" value="Off" />


#判断是否选中
	$('input[name=foo]').is(':checked')
	$('input[name=foo]').attr('checked')

#选中和不选中
	$('input[name=foo]').attr('checked', true);
	$('input[name=foo]').attr('checked', false);




判断是否选中,开始用的办法



$(function() {
  $('#isOther').click(function() {
    var other = $('#isOther:checked').val();
    if (other != undefined) $('#other').removeAttr('readonly');
    else $('#other').attr('readonly', 'readonly');
  });
});



其实可以

var checked = $('input[type=checkbox]').attr('checked');


返回一个jason对象,是有结构的
var myJSONObject = {"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

myData = JSON.parse(text, function (key, value) {
    var type;
    if (value && typeof value === 'object') {
        type = value.type;
        if (typeof type === 'string' && typeof window[type] === 'function') {
            return new (window[type])(value);
        }
    }
    return value;
});



一些参考:
都是把一组复选框是否选中情况,得到。等着ajax发出去

var sList = "";
$('input[type=checkbox]').each(function () {
    var sThisVal = (this.checked ? "1" : "0");
    sList += (sList=="" ? sThisVal : "," + sThisVal);
});
console.log (sList);

另一个

var str = "";

$(':checkbox').each(function() {
    str += this.checked ? "1," : "0,";
});

str = str.substr(0, str.length - 1);    //Remove the trailing comma

  相关解决方案