javascript - 如何根据单选按钮选择显示不同的跨度?

标签 javascript jquery radio-button

我正在尝试根据单选按钮选择显示特定的跨度消息。此外,我想在用户做出选择后立即锁定选择。换句话说:

enter image description here

我几乎能够实现我想要创建的效果: http://jsfiddle.net/baumdexterous/HkFXs/1/

但我不确定如何在选择不正确的选项时显示不正确的部分。我仍然是一个初学者程序员,不知道如何让它工作。请参阅下面我的 JS 和 HTML。希望能够了解如何实现这项工作!

HTML

      <div id="wizard">



        <div id="space" style="width:810px; background:white; height:10px;">
        </div>

        <div class="items">

            <!-- Question 1 -->
            <div class="page one">
                <h2><strong>Question 1:</strong></h2>
                <ul>
                    <li class="required double">
                        <label>
                            <div class="qselections required">
                                <input type="radio" value="a" name="question1">a) New York
                                <div id="questiononea"></div>
                                <br>
                                <input type="radio" value="b" name="question1">b) Washington DC
                                <br>
                                <input type="radio" value="c" name="question1">c) Seattle
                                <br>
                                <input type="radio" value="d" name="question1">d) Portland
                                <br>
                            </div>
                        </label>
                    </li>
                </ul>
                <li class="clearfix">
                    <button type="button" class="next right">Proceed »</button>
                </li>
            </div>

          </div>

        </div><!--items-->

      </div><!--wizard-->

    </form>

JavaScript

    $(function () {



      var root = $("#wizard").scrollable();
      var isRadioCheck = false;
      // some variables that we need
      var api = root.scrollable(),
          drawer = $("#drawer");


          // prevent the user from making another selection once one radio option has been selected
        $('input[type=radio]').click(function(){ $('input[type=radio]').prop('disabled',true); });

          // Show user correct answer
        var appended = " <span id='qselectionsCORRECT' style='font-size:16px; line-height:16px;'>Correct</span>";
        appended.id = 'appended';

        $('input:radio[name="question1"]').change(
            function(){
                if ($(this).val() == 'a') {
                    $(appended).appendTo('#questiononea');
                }
                else {
                    $(appended).remove();
                }
            });

      // validation logic is done inside the onBeforeSeek callback
      api.onBeforeSeek(function (event, i) {

          // we are going 1 step backwards so no need for validation
          if (api.getIndex() < i || $('input[type=radio]').is(':checked')) {

              // 1. get current page
              var page = root.find(".page,.qselections").eq(api.getIndex()),

                  // 2. .. and all required fields inside the page
                  inputs = page.find(".required :input").removeClass("error"),

                  // 3. .. which are empty
                  empty = inputs.filter(function () {
                      isRadioCheck = $('input[type=radio]').is(':checked');
                      return $(this).val().replace(/\s*/g, '') == '';
                  });



              //ealert('Empty Value is bool : ' + empty.length + isRadioCheck);
              if (isRadioCheck) {
                  $('.qselections').removeClass("error");
                  //alert('removed');
              }
              // if there are empty fields, then
              if (empty.length || !isRadioCheck) {

                  // slide down the drawer
                  drawer.slideDown(function () {

                      // colored flash effect
                      drawer.css("backgroundColor", "#fff");
                      setTimeout(function () {
                          drawer.css("backgroundColor", "#fff");
                      }, 1000);
                  });

                  // add a CSS class name "error" for empty & required fields
                  empty.addClass("error");
                  if (!isRadioCheck) $('.qselections').addClass("error");
                  // cancel seeking of the scrollable by returning false
                  return false;

                  // everything is good
              } else {

                  // hide the drawer
                  drawer.slideUp();
              }

          }

          // update status bar
          $("#status li").removeClass("active").eq(i).addClass("active");

      });

      // if tab is pressed on the next button seek to next page
      root.find("button.next").keydown(function (e) {
          if (e.keyCode == 9) {

              // seeks to next tab by executing our validation routine
              api.next();
              e.preventDefault();
          }
      });
    });

但是

最佳答案

为什么不使用 CSS 来修复它呢?使用 javascript,只需在您的输入中添加 true/false(或其他)类,如下所示:

  <input type="radio" value="a" class="true" />
  <input type="radio" value="b" class="false" />

对于 CSS,请使用 :after-pseudo

input.true:after {
    content:'Correct';
    color:green;
}
input.false:after {
    content:'Incorrect';
    color:red;
}

关于javascript - 如何根据单选按钮选择显示不同的跨度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938041/

相关文章:

javascript - Appcelerator/Titanium Alloy - 如何在将字符串添加到 View 之前对其进行操作

javascript - 动态创建的单选按钮不显示选中的 ASP.NET

Javascript/Jquery 不能使用 0 或 1 作为变量的值?

javascript - 如何在 JavaScript 中将字符串转换为对象数组

javascript - 如何模糊地检查字符串中的子字符串?

jquery - 通过ajax更新内容时的可排序功能

javascript - 将不同的 Span 和 Input 元素传递给 Javascript 函数

javascript - 检查多个 div 是否包含特定文本以添加 css 类

将 Shiny 的单选按钮重置为空值

javascript - arduino + 串口 + chrome + 发送 - 无法通过串口发送数据