简体   繁体   English

单击复选框时,复选框不会更改其选中状态

[英]Checkbox doesn't change its check-state when clicking on it

I'm new in ASP.NET MVC and now I faced with a problem which occurs with checkbox if it appears on page later than page was originally loaded.我是 ASP.NET MVC 的新手,现在我遇到了一个问题,如果复选框出现在页面最初加载之后的页面上。 See the next scenario: when I click let's say a button, Javascript handler calls controller post-action, and its result html is loaded into some concrete div.看下一个场景:当我点击一个按钮时,Javascript 处理程序调用控制器 post-action,其结果 html 被加载到一些具体的 div 中。 This html contains checkbox which doesn't react on mouse down event ( doesn't change its check-state ).此 html 包含不会对鼠标按下事件做出反应的复选框(不会更改其检查状态)。

Checkbox works fine if it was rendered along with the whole page, but not if it was rendered later (as a result of post-action).如果复选框与整个页面一起呈现,则它可以正常工作,但如果稍后呈现(作为后期操作的结果)则不行。 See my markup here:在此处查看我的标记:

<a name="button" onclick="handler();"></a>

On the "button" click I want to get ActionResult from a controller and put it into a div:在“按钮”上单击我想从控制器获取ActionResult并将其放入 div:

<div id="resultWillBeLoadedIn">
    <!-- here is placeholder-->
</div>

The next html will be returned from /SomeController/SomeAction and loaded into div above:下一个 html 将从 /SomeController/SomeAction 返回并加载到上面的 div 中:

<span class="check">
    <span class="checkbox">
        <input type="checkbox" name="form" value="2" />
    </span>
</span>

Here is a Javascript handler for my "button click":这是我的“按钮单击”的 Javascript 处理程序:

function handler() {
    var url = '/SomeController/SomeAction';
    $.post(url, { params... }, function (result) {
        $('#resultWillBeLoadedIn').html(result);
}

See a part of CSS for it (I'm totally not an expert in CSS, so I'm not sure if that is that one I need to show here. And... (to be honest) I'm not an author of it, so please tell me if need more):请参阅 CSS 的一部分(我完全不是 CSS 专家,所以我不确定这是否是我需要在这里展示的内容。而且......(说实话)我不是作者它,所以请告诉我是否需要更多):

.check .checkbox{
float: left;
height: 12px;
width: 12px;
display: inline-block;
margin: 3px 5px 0 0;
cursor: pointer;
background: url(../img/checkbox.png) 0 0 no-repeat;
}
.check span.disable{
float: left;
height: 12px;
width: 12px;
margin: 3px 5px 0 0;
display: inline-block;
background: url(../img/checkbox.png) 0 -24px no-repeat;
}

Please tell me what I'm missing to make checkbox working not only if it was rendered right on page loading, but not when it was rendered as a result of post back into some div.请告诉我我缺少什么以使复选框工作不仅在页面加载时正确呈现,而且在由于回发到某个 div 的结果而呈现时也没有。

You've omitted the handler for the checkbox, but you probably need to use a delegated handler for it (docs for on ).您已经省略了复选框的处理程序,但您可能需要为其使用委托处理程序(关于on 的文档)。 Essentially, you delegate the handler to a higher level element in the DOM that does remain on the page (or maybe the body element, if no lower level element will work) and supply a selector filter that is used when the handler is invoked to see if it matches the event target.本质上,您将处理程序委托给 DOM 中确实保留在页面上的更高级别元素(或者可能是 body 元素,如果没有更低级别的元素将起作用)并提供一个选择器过滤器,该过滤器在调用处理程序时使用以查看如果它匹配事件目标。 In this way you can have a handler that is applied once and works with dynamic elements that are added later.通过这种方式,您可以拥有一个应用程序并处理稍后添加的动态元素的处理程序。

$('body').on('click', 'input[type="checkbox"]', function() {
      // handler function
});

After you add the html in the callback, then hook up a click handler.在回调中添加 html 后,然后连接一个点击处理程序。

$('selectCheckbox').on('click', handler);

Given the problem its likely a CSS/Styling issue.鉴于这个问题,它可能是 CSS/样式问题。 I think some element is floating over the checkbox and preventing it from being clicked on.我认为某些元素漂浮在复选框上并阻止它被点击。 Can you look at it in chrome inspector and see what is getting hovered when you put your mouse over the checkbox?您可以在 chrome Inspector 中查看并查看将鼠标悬停在复选框上时悬停的内容吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM