jquery对于checkbox一些处理

2011-06-09 18:27

jquery对于checkbox一些处理

by

at 2011-06-09 10:27:34

original http://hlee.iteye.com/blog/1073950


<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


          <br><br>
          <span style="color:red">
            <a href="http://hlee.iteye.com/blog/1073950#comments" style="color:red">已有 <strong>0</strong> 人发表留言,猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br><br><br>

ITeye推荐