0

次のフォームを作成しました: http://jsfiddle.net/baumdexterous/GYNSu/1/

多くのフォーム要素で機能する検証スクリプトを実装しましたが、何らかの理由でラジオ ボタンでは機能しないため、現在、ユーザーは必要なラジオ ボタンのいずれかを選択せず​​にフォームを送信できます。

この既存のスクリプトを使用して、ラジオ ボタンの検証を有効にするにはどうすればよいですか? 本当にありがとう。

    <ul>
        <li class="required">
            <label>What is the U.S. Capital? Please select your answer below:</label>
            <div class="questionsone">
                <input name="questionone" type="radio" value="a">a) New York<br>
                <input name="questionone" type="radio" value="b">b) Washington DC<br>
                <input name="questionone" type="radio" value="c">c) Seattle<br>
                <input name="questionone" type="radio" value="d">d) Portland<br>
            </div>
        </li>
    </ul>

更新: 既存の検証で動作するこの JavaScript 部分があります。ラジオボタンを検出する方法を追加する方法を見つけようとしています:

    <script>
      $(function() {
          var root = $("#wizard").scrollable();
          // some variables that we need
          var api = root.scrollable(),
              drawer = $("#drawer");
          // 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) {
                  // 1. get current page
                  var page = root.find(".page").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() {
                          return $(this).val().replace(/\s*/g, '') == '';
                      });
                  // if there are empty fields, then
                  if (empty.length) {
                      // slide down the drawer
                      drawer.slideDown(function() {
                          // colored flash effect
                          drawer.css("backgroundColor", "#229");
                          setTimeout(function() {
                              drawer.css("backgroundColor", "#fff");
                          }, 1000);
                      });
                      // add a CSS class name "error" for empty & required fields
                      empty.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();
              }
          });
      });
    </script>
4

6 に答える 6

0

入力名はすべて同じである必要はありません。つまり、検証のために questionone の値が null でないことを確認します。

于 2013-11-06T17:48:18.953 に答える
0

ラジオ ボタンは 1 つの選択肢しかないため、すべてのラジオ ボタンに同じ名前を設定します。

    <input name="questionone" type="radio" value="a">a) New York<br>
    <input name="questionone" type="radio" value="b">b) Washington DC<br>
    <input name="questionone" type="radio" value="c">c) Seattle<br>
    <input name="questionone" type="radio" value="d">d) Portland<br>

jQuery を使用した検証:

if($("input[type=radio,name=questionone]:checked").length >0)
{    
  /* do something */
}
于 2013-11-06T17:55:45.977 に答える
0

最初に、ラジオ ボタンの名前をすべて同じものに変更する必要があります。元

<ul>
    <li class="required">
        <label>What is the U.S. Capital? Please select your answer below:</label>
        <div class="questionsone">
            <input name="question" type="radio" value="a">a) New York<br>
            <input name="question" type="radio" value="b">b) Washington DC<br>
            <input name="question" type="radio" value="c">c) Seattle<br>
            <input name="question" type="radio" value="d">d) Portland<br>
        </div>
    </li>
</ul>

そのため、1 つだけを選択できます。次に、document.ready() 関数を作成する必要があります。このコードを document.read() に追加します

$( document ).ready(function() {
    function RadioTest(){
    inputs = document.getElementsByName('question');
            for (index = 0; index < inputs.length; ++index) {
                if(inputs[index].checked!=true){
                                alert("radio not selected");
                                    return false;
                        }
            }

    }
});
于 2013-11-06T17:55:55.100 に答える
0

すべての無線入力タグに同じ名前を付ける必要があります。このような

<div class="questionsone">
            <input name="qstn1" type="radio" value="a">a) New York<br>
            <input name="qstn1" type="radio" value="b">b) Washington DC<br>
            <input name="qstn1" type="radio" value="c">c) Seattle<br>
            <input name="qstn1" type="radio" value="d">d) Portland<br>
     </div>
于 2013-11-06T17:56:34.370 に答える