2016-09-26 9 views
1

特定のラジオボタンをクリック/チェックすると、いくつかの要素を表示または非表示にするフォームがあります。この関数は、MVC検証がポスト時にエラーで戻ってきた場合にも、ページ・ロードで実行する必要があります。問題は私がページの読み込みにTypeError: Cannot read property 'indexOf' of undefinedを得ていることです。コードは機能しますが、スクリプトセクションの下にあるすべてのコードが壊れてしまいます。私は、ここでラジオボタンが正しく選択されているかどうかチェックしていないという結論に達しています:if ($element.attr('class').indexOf('update-type-check-current') !== -1)JQueryラジオボタンをクリックするUncaught throw TypeError:未定義の 'indexOf'プロパティを読み取ることができません

はJQuery:

$(document).ready(function() { 
    $('#CurrentEmployeeRB input[type="radio"]').click(function() { 
     clickFunc($(this)); 
    }); 
    clickFunc($('#CurrentEmployeeRB input[type=radio]:checked')); 
}); 

function clickFunc($element) { 
    var currentEmployee = $('.current-employee'); 
    if ($element.attr('class').indexOf('update-type-check-current') !== -1) { 
     currentEmployee.show(); 
    } 
    else { 
     currentEmployee.hide(); 
    } 
} 

HTML:

<div class="form-group"> 
    <label class="col-md-2 control-label" for="UpdateType">New or Current?</label> 
    <div class="col-md-10" id="CurrentEmployeeRB"> 
     <label class="radio-inline"> 
      <input class="btn btn-primary update-type-check-new" data-val="true" data-val-required="Please select whether the employee is new or current." id="UpdateType" name="UpdateType" type="radio" value="New"> New 
     </label> 
     <br> 
     <label class="radio-inline"> 
      <input class="btn btn-secondary update-type-check-current" id="UpdateType" name="UpdateType" type="radio" value="Current"> Current 
     </label> 
     <br> 
     <span class="field-validation-valid text-danger" data-valmsg-for="UpdateType" data-valmsg-replace="true"></span> 
    </div> 
</div> 

レイザー:

<div class="form-group"> 
    @Html.LabelFor(m => m.UpdateType, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10" id="CurrentEmployeeRB"> 
     <label class="radio-inline"> 
      @Html.RadioButtonFor(m => m.UpdateType, "New", new { @class = "btn btn-primary update-type-check-new" }) New 
     </label> 
     <br /> 
     <label class="radio-inline"> 
      @Html.RadioButtonFor(m => m.UpdateType, "Current", new { @class = "btn btn-secondary update-type-check-current" }) Current 
     </label> 
     <br /> 
     @Html.ValidationMessageFor(m => m.UpdateType, "", new { @class = "text-danger" }) 
    </div> 
</div> 

は、私がこれを行うべきである別の方法はありますか?私はちょうどJQueryを学んでいるところですが、これまでの検索では何の選択肢も見つけられません。

+0

達成しようとしていることは何ですか? –

+0

この行をもう一度確認してください: 'clickFunc($( '#CurrentEmployeeRB input [type = radio]:checked'))'。その行にはチェックされたラジオボタンが必要です。しかし、デフォルトでチェックされたラジオボタンはありませんでした。 –

+0

あなたのHTMLに現在の従業員としてのクラスはありません –

答えて

1

あなたはhasClassを使用し、indexOfで検索しない

if($('#CurrentEmployeeRB input[type=radio]:checked').length >= 1) { 
    clickFunc($('#CurrentEmployeeRB input[type=radio]:checked')); 
} 
+0

パーフェクト!あなたはまずそれに気づいた。間違いなく私がここで何が起こっていたのかを論理的に理解する助けになりました! – justiceorjustus

+0

@eisbehrと同様に、hasClassで確認することをお勧めします。 – yomisimie

+0

悪い考え。 OPはデフォルトでチェックされたラジオボタンを必要としないと言った。つまり、if文の条件は決して真実ではありません。また、ラジオボタンをチェックするたびにではなく、ページロード後に1回チェックします。 –

1

で準備

clickFunc($('#CurrentEmployeeRB input[type=radio]:checked')); 

で行を変更する必要があります。セレクタによって要素が返されなかった場合、コードは失敗します。

function clickFunc($element) { 
    var currentEmployee = $('.current-employee'); 

    if($element.hasClass('update-type-check-current')) { 
     currentEmployee.show(); 
    } 
    else { 
     currentEmployee.hide(); 
    } 
} 

だけヒント:あなたも同じタスクのためtoggleを使用することができます。

function clickFunc($element) { 
    $('.current-employee').toggle($element.hasClass('update-type-check-current')); 
} 
1

まず$('#CurrentEmployeeRB input[type="radio"]:checked')$('#CurrentEmployeeRB input[type=radio]:checked')を変更し、最初に文書をロードした後、何のラジオボタンがチェックされていないので、$elementは何も含まれていません。あなたはそのエラーが発生するので、if statementの中にロジックを入れてください。

if($element.length != 0) {} 

JS:

$(document).ready(function() { 
    $('#CurrentEmployeeRB input[type="radio"]').click(function() { 
     clickFunc($(this)); 
    }); 
    clickFunc($('#CurrentEmployeeRB input[type="radio"]:checked')); 
}); 

function clickFunc($element) { 
    if($element.length != 0) { 
    var currentEmployee = $('.current-employee'); 
    console.log($element); 
    if ($element.attr('class').indexOf('update-type-check-current') !== -1) { 
     currentEmployee.show(); 
    } 
    else { 
     currentEmployee.hide(); 
    } 
     } 
} 
関連する問題