当单选按钮运行于=“服务器”时,使用 jQuery 检查单选按钮?

发布于 2024-09-13 08:05:36 字数 585 浏览 5 评论 0原文

使用 jQuery 我希望能够单击一个元素,该元素还将检查其相关的单选按钮。我一直工作正常,直到我们必须将 runat="server" 添加到单选按钮。

当我应用这个时,它会阻止我的 jQuery 函数工作,并且我不知道如何绕过它,这是代码的简化版本:

HTML

<input type="radio" runat="server" id="sector1Radio" name="SectorGroup" title="Sector1" />

jQuery

$('#SomethingElse').click(function() {
    $('input[title=Sector1]').attr('checked','checked');
});

我发现当它转换为 .net 控件而不是 check= 时“已检查”(通常是这样)它只是已检查,所以我改变了这一点,但在多个浏览器中检查 DOM 时,我的单选按钮都没有被检查:-(

还有其他方法可以使用 jQuery 来检查吗一个带有 runat="server" 的单选按钮

Using jQuery I want to be able to click an element which will also checks it's related radio button. I had this working fine until we had to add runat="server" to the radio buttons.

When I apply this it prevents my jQuery function from working and I cant figure out how to get round it, heres a simplified version of the code:

HTML

<input type="radio" runat="server" id="sector1Radio" name="SectorGroup" title="Sector1" />

jQuery

$('#SomethingElse').click(function() {
    $('input[title=Sector1]').attr('checked','checked');
});

I've found out that when its converted to a .net control instead of checked="checked" (as it would be usually) it is just Checked, so I changed that but on inspecting the DOM in multiple browsers, none of my radio buttons are being checked :-(

Are there any other ways I can use jQuery to check a radio button that has runat="server"?

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

思念绕指尖 2024-09-20 08:05:37

正如其他人所建议的,ASP.net 不会生成具有与您指定的相同 ID 的 html。

快速解决方案:

  • 您可以继续使用 id,但要求 jquery 检查 id 的末尾,例如:

    $("input[id$='sector1Radio']").is(":checked");

  • 或者根据Nico建议检查标题和名称

  • 使用不受ASP.net影响的类元素,例如

    $("input.sector1Radio").is(":checked");

最好的办法是查看生成的 html 代码并查看 id 为您提供的内容,然后您可以使用适当的 jquery 选择器,因为生成的 id 可能具有不同的扩展名,具体取决于您是否使用母版页等。

As suggested by others, ASP.net will not generate the html with the same ID you specified.

Quick solutions:

  • You can keep using the id but asks jquery to check the end of the id instead, example:

    $("input[id$='sector1Radio']").is(":checked");

  • Or check against the title and name as Nico suggested

  • Use the class element which is not effected by ASP.net, example

    <input type="radio" runat="server" id="sector1Radio" class="sector1Radio" name="SectorGroup" title="Sector1" />

    $("input.sector1Radio").is(":checked");

Best thing is to view the generated html code and see what id is giving you, then you can use the appropriate jquery selector, because the generated id could have different extensions depends whether you use master pages, etc.

把昨日还给我 2024-09-20 08:05:37

如果您使用 MasterPage 或动态创建控件,则控件 ID 可能会被重命名为#SomethingElse 变为#MainContent_SomethingElse。

检查这一点的最简单方法是使用 Firefox 或 Chrome 的 WebDeveloper 插件。
转到信息->显示元素信息,然后选择有问题的对象。它会给你它的 ID、类以及祖先和子代信息。
检查 .NET 是否动态更改 ID。

如果是这种情况:

为了防止这种情况,在服务器端代码中,您可以使用以下属性来创建静态 ID,

SomethingElse.ClientIDMode = ClientIDMode.Static;

然后您可以在 jQuery 中引用

$('#SomethingElse').click(function() {
            if ($('input[title=Sector1]').attr('checked')) {
             //execute event
});

If you are using a MasterPage or are creating the controls dynamically then it is probable that the control ID's are being renamed #SomethingElse becomes #MainContent_SomethingElse.

The easiest way to check this is to use the WebDeveloper plugin for Firefox or Chrome.
Go to Information -> Display Element Information and then select the object in question. It will give you it's ID, class, as well as ancestor and children information.
Check to see if the ID is being changed dynamically by the .NET.

If that's the case:

To prevent this, in the server side code you can use the following attribute to create static ID's

SomethingElse.ClientIDMode = ClientIDMode.Static;

You can then reference in you jQuery

$('#SomethingElse').click(function() {
            if ($('input[title=Sector1]').attr('checked')) {
             //execute event
});
冷血 2024-09-20 08:05:37

我认为发生的情况是,在 ASP NET 复选框和单选按钮中生成一个“输入”和输入后的一个“范围”。所以你只需要选择输入。

您可以尝试:

$('.classname input[type=checkbox]').each(function() {
        this.checked = true;
});

I think what happens is that in ASP NET Checkboxes and Radio Buttons generates an "input" and a "span" after the input. So you need to select the input only.

You can try:

$('.classname input[type=checkbox]').each(function() {
        this.checked = true;
});
ら栖息 2024-09-20 08:05:37

这里有两件事:找到控件并执行检查。在 ASP.NET 中,控件的实际 ID 和名称最终将根据它所在的 runat="server" 容器进行更改,即使这些容器没有 ID。

呈现的 ASP.NET 控件始终以与开始时相同的名称结尾,因此像这样的标记

<input type="radio" runat="server" id="sector1Radio" title="Sector1" />

可能最终呈现为

<input type="radio" runat="server" id="ctl0$ctl0$sector1Radio" name="ctl0_ctl0_SectorGroup" title="Sector1" />

You can find this element,即使在呈现后(如果您在 JQuery 中使用“包含”选择语法)也是如此。因此,要找到此元素,一旦呈现,您可以使用:

$("input[type='radio'][id*='$sector1Radio']")

此语法将查找 id 包含“$sector1Radio”的任何单选按钮

一旦您拥有该元素,您可以使用以下代码来选中或取消选中它,您可以从中调用其他元素的点击事件。

// check the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', true);    

// uncheck the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', false);   

最后一件事...如果您只想在按下时单击按钮的文本块(将其包装在标签中并将 AssociatedControlId 属性设置为单选按钮的控件名称,如下所示...

<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
<asp:label runat="server" id="lblsector1Radio" associatedControlID="sector1Radio">clicking here clicks and unclicks the radio button</asp:label>

Two things here: finding the control and executing the check. In ASP.NET, your control's actual ID and name will end up getting changed based on the runat="server" containers in which it appears, even if those containers have no Ids.

Rendered ASP.NET controls always end with the same name as you started with, so a tag like:

<input type="radio" runat="server" id="sector1Radio" title="Sector1" />

might end up being rendered as

<input type="radio" runat="server" id="ctl0$ctl0$sector1Radio" name="ctl0_ctl0_SectorGroup" title="Sector1" />

You can find this element, even after it is rendered if you use the "contains" selection syntax in JQuery. So to find this element, once rendered, you could use:

$("input[type='radio'][id*='$sector1Radio']")

This syntax will find any radio button whose id contains "$sector1Radio"

Once you have the element, you can check or uncheck it using the following code, which you'd call from the click event of your other element.

// check the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', true);    

// uncheck the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', false);   

One last thing... if you just want a block of text to click the button when pressed (wrap it in an tag and set the AssociatedControlId property to the control name of your radio button, like this...

<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
<asp:label runat="server" id="lblsector1Radio" associatedControlID="sector1Radio">clicking here clicks and unclicks the radio button</asp:label>
岁月打碎记忆 2024-09-20 08:05:37

我也有同样的问题。要使用 jQuery UI 使您的 radiobuttons 变得漂亮,必须这样写:

<div id="radio">
<input type="radio" id="radio1" runat="server" />
<label for="radio1">The label of the radio button</label>
...
</div>

<script type="text/javascript">
$('#radio').buttonset();
</script>

输入标签的 id 必须由标签的 for 属性正确引用。如果网页位于母版页内,则输入标记的 id 将被修改为类似 ctl00_Something_radio1 的内容,并且标签的 for 属性突然不再引用输入标记。在 ASP.NET 中要小心这一点!

I had the same problem. To use the jQuery UI to make your radiobuttons nice one has to write:

<div id="radio">
<input type="radio" id="radio1" runat="server" />
<label for="radio1">The label of the radio button</label>
...
</div>

<script type="text/javascript">
$('#radio').buttonset();
</script>

The id of the input tag must be correctly referenced by the label's for attribute. If the webpage is inside a master page then the id of the input tag will be modified to something like ctl00_Something_radio1, and suddenly the label's for attribute no longer references the input tag. Beware of this in ASP.NET!

梦屿孤独相伴 2024-09-20 08:05:36

我认为你的问题是输入的id不再是sector1Radio而是ctl00_sector1Radio或类似的东西。如果您的输入控件位于 ContentPlaceHolder 控件内部(使用母版页时),则会发生这种情况。

您可以检查生成的 HTML 代码(在浏览器中)来验证是否是这种情况吗?输入控件的 id 是什么?

如果是这种情况,您需要

$('#SomethingElse').click(function() {
  $('input[title=Sector1]').attr('checked','checked');
});

从代码隐藏生成您的 js jQuery 代码,以便将 SomeThingElse 替换为控件的 ClientID。

I think that Your problem is that the id of the input is no longer sector1Radio but rather ctl00_sector1Radio or something similar. This happens if Your input control is inside e.g. a ContentPlaceHolder control (when using master pages).

Can You check the generated HTML code (in the browser) to verify if this is the case? What is the id of the input control?

If this is the case, You need to generate Your js jQuery code

$('#SomethingElse').click(function() {
  $('input[title=Sector1]').attr('checked','checked');
});

from codebehind so that SomeThingElse is replaced with the ClientID of the control.

你如我软肋 2024-09-20 08:05:36

.is(':checked') 适用于 ASP.NET 单选按钮和复选框

$('#SomethingElse').click(function() {
    $('input[title=Sector1]').is(':checked');
});

.is(':checked') works on ASP.NET radiobuttons and checkboxes

$('#SomethingElse').click(function() {
    $('input[title=Sector1]').is(':checked');
});
与之呼应 2024-09-20 08:05:36

尝试使用

$('input[title=Sector1]').attr('checked',true);

and

$('input[title=Sector1]').attr('checked',false);

或者也许

$('#SomethingElse').click(function () {
    $('input[title=Sector1]').attr('checked',!$('input[title=Sector1]').attr('checked'));
});

try using

$('input[title=Sector1]').attr('checked',true);

and

$('input[title=Sector1]').attr('checked',false);

or maybe

$('#SomethingElse').click(function () {
    $('input[title=Sector1]').attr('checked',!$('input[title=Sector1]').attr('checked'));
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文