Radiobuttons和Checkboxes在Javascript中"检查"了问题

Jea*_*ini -1 javascript jquery json zurb-foundation

我有Javascript代码JQuery用于创建包含自由文本,单选按钮(单选)和复选框(多选)问题的测验.测验是使用Web界面制作的,Zurb - Foundation用于样式,并在JSON中进行序列化.在创建单选按钮和特定问题的复选框答案时,当用户检查任一组件时(例如,将其标记为有效答案),它应该验证这一点,并且变为"true"(由JSON中的数字"1").

它目前正在处理文本类型问题,因为它基本上是硬编码的.但它并没有为其他两个做伎俩.

这是主要的代码片段(如果需要更多,我将编辑问题):整个测验

storeQuiz: function( event ) {
      var self = event.data;
      var store = [];

      $(self.element).find( '.question-content' ).each( function(){
        var question = $( this );
        var entry = { options: [] };

        if ( question.parent().attr( 'class' ).match( /template/ ) ) {
          return true;
        }

        entry['content'] = question.find( '.input' ).val();
        entry['type'] = question.parent().attr( 'class' ).match( /quiz-(\w+)/ )[1];

        question.find( '.option' ).each( function() {
          var option = $( this );
          var data = {};

          if ( entry.type === 'text' ) {

            data['valid'] = true;
          } else {
            data['valid'] = !!option.find( '.option-validation input' ).attr( 'checked' );
          }

          data['content'] = option.find( '.option-content textarea' ).val();

          entry.options.push( data );
        })

        store.push( entry );
      });

      self.storeUpdate( store );
    },
Run Code Online (Sandbox Code Playgroud)

收音机:

buildRadios: function( data ) {
      var tmpl = this.radiosHandler( {data: this} );
      var self = this;

      tmpl.find( '.option' ).remove();
      tmpl.find( '.input' ).val( data.content );

      $.each( data.options, function() {
        var plus = tmpl.find( '.add' );
        var option = self.addAnswer.call( plus, {data: self} );
        option.find( '.option-validation input' ).attr( 'checked', this.valid );
        option.find( '.option-content textarea' ).val( this.content );
      });
    },
Run Code Online (Sandbox Code Playgroud)

复选框:

   buildCheckboxes: function( data ) {
      var tmpl = this.checkboxesHandler( {data: this} );
      var self = this;

      tmpl.find( '.option' ).remove();
      tmpl.find( '.input' ).val( data.content );

      $.each( data.options, function() {
        var plus = tmpl.find( '.add' );
        var option = self.addAnswer.call( plus, {data: self} );
        option.find( '.option-validation input' ).attr( 'checked', this.valid );
        option.find( '.option-content textarea' ).val( this.content );
      });
    },
Run Code Online (Sandbox Code Playgroud)

Mai*_*KaY 6

如果你使用它会更聪明

$('input[type="checkbox"]').is(':checked');
Run Code Online (Sandbox Code Playgroud)

返回值是布尔值