我有以下伪代码例如:我想使用相同的ajax脚本来选择每条记录的phone_type和方法,我尝试使用.attr()
<script>
$('#phone_type').on('change', function (e) {
console.log(e);
var phone_type = e.target.value;
//ajax
$.get('../get_conversion_method_by_phone_type?phone_type=' + phone_type, function (data) {
//success data
$('#method').empty();
$('#method').append('<option value="0" selected>Choose Method</option>');
$.each(data, function (index, conMethObj) {
$('#method').append('<option value="' + conMethObj.id + '">' + conMethObj.method + '</option>');
});
});
});
</script>
foreach($foo as $bar){
<tr>
<select id="phone_type" name="phone_type">
<option>Choose</option>
</select>
<select id="method" name="method">
</select>
</tr>
}
答案 0 :(得分:2)
我希望我能正确理解你想做什么,试试这段代码
<script>
$('.phone_type').on('change', function (e) {
console.log(e);
var $self = $(this);
var phone_type = e.target.value;
//ajax
$.get('../get_conversion_method_by_phone_type?phone_type=' + phone_type, function (data) {
//success data
$method = $self.next('.method');
$method.empty();
$method.append('<option value="0" selected>Choose Method</option>');
$.each(data, function (index, conMethObj) {
$method.append('<option value="' + conMethObj.id + '">' + conMethObj.method + '</option>');
});
});
});
</script>
和html
foreach($foo as $bar){
<tr>
<select class="phone_type" name="phone_type">
<option>Choose</option>
</select>
<select class="method" name="method">
</select>
</tr>
}