最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc 3 - Javascript to uncheck a checkbox when another is checked - Stack Overflow

programmeradmin1浏览0评论

This is a MVC 3 application that uses the razor view engine. I have the following line of code in a view that displays 2 checkboxes. When one is checked the other checkbox should uncheck. I have searched around and between my lack of exp with javascript and the lack of threads found on google related to this i am at a lost for even a code snip of javascript to use for a worthwhile starting point. Any ideas?? This below is what I have coded up on my own but am more stuck than anything with this.

        $("#isStaffCheckBox").change(
              function (e) {
                  var checked = $(this).attr('checked');
                  if(checked)
                  {

    <li>Staff Member: @Html.CheckBoxFor(Function(f) f.isStaff, New With {.id = "isStaffCheckBox"}) Board Member: @Html.CheckBoxFor(Function(f) f.boardMember, New With {.id = "isBoardCheckBox"})</li>

Any help is greatly appreciated..

This is a MVC 3 application that uses the razor view engine. I have the following line of code in a view that displays 2 checkboxes. When one is checked the other checkbox should uncheck. I have searched around and between my lack of exp with javascript and the lack of threads found on google related to this i am at a lost for even a code snip of javascript to use for a worthwhile starting point. Any ideas?? This below is what I have coded up on my own but am more stuck than anything with this.

        $("#isStaffCheckBox").change(
              function (e) {
                  var checked = $(this).attr('checked');
                  if(checked)
                  {

    <li>Staff Member: @Html.CheckBoxFor(Function(f) f.isStaff, New With {.id = "isStaffCheckBox"}) Board Member: @Html.CheckBoxFor(Function(f) f.boardMember, New With {.id = "isBoardCheckBox"})</li>

Any help is greatly appreciated..

Share Improve this question asked May 26, 2013 at 1:17 Skindeep2366Skindeep2366 1,5593 gold badges41 silver badges69 bronze badges 4
  • 2 Why are you reinventing a radio button? – epascarello Commented May 26, 2013 at 1:20
  • As far as I know in razor view radio button or checkboxes still need javascript to handle click event to uncheck another. – Skindeep2366 Commented May 26, 2013 at 4:04
  • 1 Why would a radio button need JavaScript? Give them the same name and they magically uncheck. – epascarello Commented May 27, 2013 at 2:41
  • I will look at this a bit more in detail shortly.. Thanks much for the pointer. – Skindeep2366 Commented May 30, 2013 at 1:37
Add a ment  | 

3 Answers 3

Reset to default 5

If there is some reason for this to be a checkbox instead of a radio button then the following code should do it.

    $("#isStaffCheckBox").change(
          function (e) {
              $("#isBoardCheckBox").prop('checked',!this.checked);
          }
    });

Change the property "("#isBoardCheckBox").prop" to "("#isBoardCheckBox").attr".

Works fine!

  $("#isStaffCheckBox").change(
  function (e) {
      $("#isBoardCheckBox").attr("checked", !this.checked);
  })

// If you use jquery version 1.6.4 or below use "attr", if you use higher versions (1.9 or above) use "prop" instead of attr.

Code:

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Checkbox</title>
        <script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $(":checkbox").change(function () {
                    $("input:checkbox").attr("checked", false);
                    $(this).attr("checked", "checked");
                });
            });
        </script>
    </head>
    <body>
        <input type="checkbox" id="chk1" value="1" /> Staff Member  <br/>
        <input type ="checkbox" id="chk2" value="2" /> Board Member
    </body>
</html>

Please have a look on this link JsFiddle my code

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>