programmer138200 programmer138200 - 6 months ago 9
HTML Question

How to select an element with find statment?

I try to fill

select
element with
json
data but I have a problem .I can not select target element right. I have not any error in console . Please advice.

jquery :

$('#accordion1 select[name$=Tarrif]').change(function () {


$.ajax({
url: '/Train/TrianFood',
data: { Tarrif: $(this).val() },
type: "POST",
success: function (result) {

if (result.Result == false) {

for (var i = 0; i <result.obj.length; i++) {
$(this).closest('.person-information').find('select[name$=Food]').empty().append(new Option(result.obj[i].ServiceType, result.obj[i].ServiceTypeCode));
}
} else {
$(this).closest('.person-information').find('select[name$=Food]').empty().append(new Option('بدون غذا', 1));
}

},
error: function () { }
});
//-------------
});





<div class="panel style1">
<h4 class="panel-title">
<a href="#acc@(j)" data-toggle="collapse" data-parent="#accordion1">مسافر @(j + 1) </a>
</h4>
<div class="panel-collapse collapse @if(j==0) {<text> in </text>}" id="acc@(j)">
<div class="panel-content">
<div class="person-information">
<div class="form-group row">
<div class="col-md-4 col-md-push-8">
<label>نام </label>
<input type="text" name="[@(j)].FirstName" class="input-text full-width" lang="fa-IR" data-val="true" data-val-required="لطفا نام را وارد نمایید." />
<span class="field-validation-valid" data-valmsg-for="[@(j)].FirstName" data-valmsg-replace="true"></span>
</div>
<div class="col-md-4">
<label>نام خانوادگی</label>
<input type="text" name="[@(j)].LastName" class="input-text full-width" lang="fa-IR" data-val="true" data-val-required="لطفا نام خانوادگی را وارد نمایید." />
<span class="field-validation-valid" data-valmsg-for="[@(j)].LastName" data-valmsg-replace="true"></span>
</div>
<div class="col-md-4 col-md-pull-8">
<label>کد ملی</label>
<input type="text" name="[@(j)].NationalCode" class="input-text full-width" lang="fa-IR" data-val-required="لطفا کد ملی را وارد نمایید." placeholder="کد ملی 10 رقمی" data-val="true" data-val-length="کد ملی باید 10 رقمی باشد." data-val-length-max="10" data-val-length-min="10" data-val-regex="فقط عدد وارد نمایید." data-val-regex-pattern="(^[0-9']+$)" />
<span class="field-validation-valid" data-valmsg-for="[@(j)].NationalCode" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group row">
<div class="col-md-4 datepicker-wrap">
<label>تاریخ تولد</label>
<input type="text" name="[@(j)].DateOfbirth" autocomplete="off" class="input-text full-width" placeholder="--/--/----" data-val="true" data-val-required="لطفا تاریخ تولد را وارد نمائید." />
<span class="field-validation-valid" data-valmsg-for="[@(j)].DateOfbirth" data-valmsg-replace="true"></span>
</div>
<div class="col-md-4 col-md-push-4">
<label>جنسیت</label>
<div class="selector">
<select class="full-width" name="[@(j)].SexCode">
<option value="1">مرد</option>
<option value="2">زن</option>
<option value="3">خانواده</option>
</select>
</div>
</div>
<div class="col-md-4 col-md-pull-4">
<label>نوع مسافر</label>
<div class="selector">
<select class="full-width" name="[@(j)].Tarrif">
@if (TempData["Tarrif"] != null)
{
List<GetAgentAllowedTarrif> lists = new List<GetAgentAllowedTarrif>();
lists = (List<GetAgentAllowedTarrif>)TempData["Tarrif"];
foreach (var item in lists)
{
<option value="@item.FTariffCode"> @item.TarriffName</option>
}
}
</select>
</div>

</div>
</div>
<div class="row">
<div class="col-md-4 pull-right">
<label>غذا</label>
<div class="selector">
<select class="full-width" name="[@(j)].Food">
<option>بدون 1</option>
<option value="2">بدون 2</option>
</select>
</div>
</div>
</div>
</div>
</div><!-- end content -->
</div>
</div>

Answer

Try this,

Store this into current variable before call to AJAX request and then replace this which you have written inside success block with current

May this will help you..