プルダウンの内容に基づいてAjaxでデータを取得します。
その配列が空かどうかでラジオボタンを1番目、2番目で切り替えたいのですが、
切り替わらず、四苦八苦しています。
どのように記載すれば良いでしょうか?
attrとpropを用いた情報が見つかりましたが、どちらが正しいかもよくワカリマセン。
宜しくお願いいたします。
$(function(){ //プルダウンに変更があったとき $('#child').change(function(){ var child = $("#child").val(); $.ajax({ type: "POST", url: "/admin/Au042", data: {"child":child}, dataType: "json", success: function(data, status){ var j = 0; $.each(data,function(i) { if(data[i] == "0"){ $("#" + i).hide(); }else if(data[i] == "1"){ $("#" + i).show(); } j++; }); //jが0より大きいとき if(j > 0){ $('#general').hide(); $('#order').show(); //type1を選択状態に $('#type1').prop('checked', false); $('#type2').prop('checked', true); // $('#type2').attr('checked','checked'); }else{ $('#general').show(); $('#order').hide(); //type2を選択状態に $('#type2').prop('checked', false); $('#type1').prop('checked', true); // $('#product_type1').attr('checked','checked'); } }, error: function(XMLHttpRequest, status, errorThrown){ alert("エラーが発生しました!"); } }); }); });
HTML
<table> <tbody> <tr> <th>カテゴリー</th> <td> <select name="parent" id="parent"> <option value ="101">parent-test1</option> <option value ="002">parent-test2</option> <option value ="001">test001</option> </select> <select name="child" id="child"> <option value ="101001">child-test1-1</option> <option value ="101002">child-test1-2</option> </select> </td> </tr> <tr> <th>製品種別</th> <td> <input type="radio" name="ptype" id="type1" class="type" value="1" checked="checked" />商品群A <input type="radio" name="ptype" id="type2" class="type" value="2" />商品群B </td> </tr> </tbody> </table>
回答3件
あなたの回答
tips
プレビュー