简体   繁体   English

jQuery 不响应单选检查

[英]jQuery does not respond to radio checked

I want a box to appear in jQuery when I check the radio, but I did not mark and nothing happens.当我检查收音机时,我希望在 jQuery 中出现一个框,但我没有标记并且没有任何反应。

Here are the codes.这是代码。

HTML: HTML:

<script src="https://sites.google.com/site/lightdownloads154/jquery.alerts.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://sites.google.com/site/lightdownloads154/jquery.alerts.css" type="text/css" />

<center><textarea rows="10" cols="17" style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);" wrap="soft">- Todos os links e arquivos que se encontram no blog, estão hospedados na própria Internet, somente indicamos onde se encontra.

- Qualquer arquivo protegido por algum tipo de lei deve permanecer, no máximo, 24 horas em seu computador.

- Eles podem ser baixados apenas para teste, devendo o usuário apagá-lo ou compra-lo após 24 horas.

- A aquisição desses arquivos pela internet é de única e exclusiva responsabilidade do usuário.

- Os donos, webmasters e qualquer outra pessoa que tenha relacionamento com a produção do blog não tem responsabilidade alguma sobre os arquivos que o usuário venha a baixar e para que ira utiliza-los.
</textarea><center>
<center>
<input name="accept" type="radio" id="accept" /> Eu aceito <input name="refuse" type="radio" id="refuse" /> Não aceito </center></center>
</center>

​ JavaScript:​ JavaScript:

  $(document).ready(function() {

      $("#accept").click(function() {
          jAlert($('input[name=accept]:checked').attr('Obrigado por aceitar os Termos.'));
      });

      $("#refuse").click(function() {
          jAlert('refuse', 'Se não aceita os termos de uso retire-se do blog.', 'Termos de uso');
      });
  });

Demo演示

This basically fixes your code: http://jsfiddle.net/XkBSv/7/这基本上修复了您的代码:http: //jsfiddle.net/XkBSv/7/

  • jAlert is not defined in your script, you propably haven't included an external script for that. jAlert 未在您的脚本中定义,您可能没有为此包含外部脚本。
  • jAlert is causing a syntax error. jAlert 导致语法错误。
  • you haven't closed your ready event causing a syntax error.您还没有关闭导致语法错误的 ready 事件。

you probably also want to change the input names to the same value so they can actually act as radio buttons.您可能还想将输入名称更改为相同的值,以便它们实际上可以充当单选按钮。

UPDATE更新

The syntax error in jAlert is probably because it has no ; jAlert 中的语法错误可能是因为它没有; before the closure, if I add the code before your code instead of a link in the html it does work!在关闭之前,如果我在您的代码之前添加代码而不是 html 中的链接,它确实有效!

see http://jsfiddle.net/XkBSv/13/http://jsfiddle.net/XkBSv/13/

  1. Google is redirecting the requests to these files to an HTML file which is why you are getting the error: uncaught SyntaxError: Unexpected token < or not experiencing any JavaScript execution past this point. Google 将对这些文件的请求重定向到 HTML 文件,这就是您收到错误的原因: uncaught SyntaxError: Unexpected token <或在此之后没有经历任何 JavaScript 执行。

    https://sites.google.com/site/lightdownloads154/jquery.alerts.js https://sites.google.com/site/lightdownloads154/jquery.alerts.css https://sites.google.com/site/lightdownloads154/jquery.alerts.js https://sites.google.com/site/lightdownloads154/jquery.alerts.css

    Use the following:使用以下内容:

    http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.js http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.css http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.js http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.css

  2. This line does nothing:这条线什么都不做:

    $('input[name=accept]:checked').attr('Obrigado por aceitar os Termos.')

    jQuery is looking for an attribute with the name Obrigado por ... and since it doesn't exist is returning nothing. jQuery 正在寻找一个名为Obrigado por ...的属性,因为它不存在,所以什么也不返回。

  3. You need to group radio buttons with the same name attribute and differentiate by using the value attribute.您需要将具有相同 name 属性的单选按钮分组,并使用 value 属性进行区分。

Demo: http://jsfiddle.net/iambriansreed/MXZJJ/演示:http: //jsfiddle.net/iambriansreed/MXZJJ/

try this:尝试这个:

    <!DOCTYPE html>
        <html>
        <head>
            <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
            <link href="http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.css" rel="stylesheet" type="text/css"/>

            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
            <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
            <script src="http://labs.abeautifulsite.net/archived/jquery-alerts/demo/jquery.alerts.js"></script>

            <script>
                $(document).ready(function() {

                    $("#accept").change(function() {
                        if($(this).attr("checked")){
                            jAlert('This is a custom alert box', 'Radio accept selected');
                        }

                    });

                    $("#refuse").change(function() {
                        if($(this).attr("checked")){
                            jAlert('This is a custom alert box', 'Radio refuse selected');

                        }

                    });


                });

            </script>
        </head>
        <body style="font-size:62.5%;">

        <input  type="radio" name = "radioGroup" id="accept" value="accepted" /> Accepted
<input  type="radio" name = "radioGroup" id="refuse" value="rejected" /> Not Accepted </center></center>


        </body>
        </html>

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

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