简体   繁体   English

如何检测此单选按钮是否已选中?

[英]How to detect if this radiobutton is checked?

I read a lot of things on stackoverflow, but nothing help me :( 我在stackoverflow上阅读了很多东西,但没有任何帮助我:(

I have this HTML table, loaded by ajax request : 我有这个HTML表,由ajax请求加载:

<table class="table table-striped table-hover choix_annonce_table">
    <thead>
        <tr>
            <th>Sélection</th>
            <th>Visuel</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="radio" value="45" name="idAnnonce"></td>
            <td><img alt="Annonce 1" src=""></td>
        </tr>
        <tr>
            <td><input type="radio" value="46" name="idAnnonce"></td>
            <td><img alt="Annonce 2" src=""></td>
        </tr>        
    </tbody>
</table>

I try to detect when radiobutton is checked, but no of the following issues work (in js file included on my "main" page): 我尝试检测何时选中单选按钮,但以下所有问题均不起作用(在“主”页面上的js文件中):

$(document).ready(function() {
    $("input[name=idAnnonce]").click(function(){
        alert("xx");
    });
});

OR 要么

$(document).ready(function() {
    $("input[name=idAnnonce]:radio").change(function () {
        alert("xx");
    });
});

Do you have in idea? 你有主意吗?

EDIT : when I load JS directly into my loaded table with ajax, it works. 编辑:当我使用ajax将JS直接加载到加载的表中时,它可以工作。 Why ? 为什么呢

This happens because the .click() and .change() methods, along with all other event handlers, only watch for these events on elements that are present at the time the events are attached . 发生这种情况是因为.change() .click().change()方法以及所有其他事件处理程序仅监视在附加事件时存在的元素上的这些事件。

To solve this, instead of using this: 要解决此问题,请使用以下方法:

$('input[name=idAnnonce]').change(function() {
    // ...
});

Use something like this instead: 使用类似这样的东西:

/* Use 'body' or any element that will contain the form */
$('body').on('change', 'input[name=idAnnonce]', function() {
    // ...
});

This will watch for click events passing up to the body, and only call the function for those that match the selector. 这将监视传递到主体的单击事件,并且仅为与选择器匹配的事件调用函数。

If your javascript is loaded directly into the html file, then it's being executed in line as the html file is loaded and parsed. 如果您的javascript直接加载到html文件中,则在加载和解析html文件时将按行执行。 When the javascript is in a separate file, it needs to be invoked. 当javascript位于单独的文件中时,需要对其进行调用。 You could do this by executing an "onload" function as part of your body tag statement. 您可以通过在主体标签语句中执行“ onload”功能来做到这一点。 That part of your html is missing, so it's unclear whether you're actually loading anything when your table is loaded. 的那部分html丢失了,因此尚不清楚在加载表时是否实际加载了任何内容。
You can also execute these event monitors through a callback at the end of the ajax load. 您也可以在ajax加载结束时通过回调执行这些事件监视器。

When loading the table via ajax, you will need to bring in the javascript through the ajax call. 通过ajax加载表格时,您需要通过ajax调用引入javascript。 Meaning the table that comes in should also contain the javascript that references the table. 表示其中的表格还应该包含引用该表格的javascript。 The DOM on the parent page doesn't know about the table, so the javascript on the parent page won't act on new content. 父页面上的DOM不了解该表,因此父页面上的javascript不会对新内容起作用。

$(document).ready(function() {
  $('input[type=radio][name=idAnnounce]').change(function(evt) {
  console.log(evt.target.value)
  });
});

The selector you were using was wrong. 您使用的选择器错误。

The above code should help 上面的代码应该有所帮助

cheers 干杯

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

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