我正在尝试获取具有特定 id 的元素的类,如下所示。我将有多个字段生成两个带有“添加新”选项的下拉列表,但选项有不同的类,第一种情况是“animal_species”,第二种情况是“animal_condition”。在下拉列表中选择“添加新”时,会弹出一个模式窗口,其中包含一个文本字段,可在其中向下拉列表输入新选项。这是模态窗口:
<!-- add-new field -->
<div class="modal small hide fade" id="add-new" tabindex="-1" role="dialog" aria-labelledby="add-new-fieldLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">√ó</button>
<h3 id="add-new-fieldLabel">Add New Field</h3>
</div>
<div class="modal-body">
<p>Would you like to add a new item?</p>
<input type="text" id="add-new-text" name="add-new-text" placeholder="Type the new option">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="add-new-submit" name="add-new-submit"/>Add</button>
</div>
</div><!-- /add-new field -->
文本字段的 jQuery 是
$('#upload_form option[value="addnew"]').click(function(){
// Show modal window
$('#add-new').modal('show');
$('#add-new-submit').on('click', function(){
// Get new option from text field
var value = $('#add-new-text').val();
console.log(value);
// Get the class
//var Classofentry = $('#upload_form option[class]').attr("class");
var Classofentry = $('#upload_form option[class]').attr("class");
console.log(Classofentry);
$.ajax({
type: "POST",
url: "<?php echo site_url(); ?>main/change_options",
data: {new_option: value, new_option_class: Classofentry},
dataType: "html",
error: errorHandler,
success: success
});
function success(data){
//$('#animal_species').append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
$('#'+Classofentry).append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
alert(data);
//alert('Success!');
}
function errorHandler(){
alert('Error with AJAX!');
}
$('#add-new').modal('toggle');
});
});
</script>
效果很好,除了现在我添加了第二个“Animal Condition”元素,在模式中输入的文本被放入第一个(“Animal Species”)元素中,因为“Classofentry”始终是“animal_species”而不是生成模态的元素的类。如何在 jQuery 中访问该类?或者也许我需要对多个字段进行不同的实现?尝试了很多东西,但还没有到位。帮助!
这是我如何让它工作的(感谢您的评论!):
<script type="text/javascript">
$('#upload_form option[value="addnew"]').click(function(){
// Show modal window
$('#add-new').modal('show');
// Get the class
//var Classofentry = $('#upload_form option[class]').attr("class");
var Classofentry = $(this).attr("class");
console.log(Classofentry);
$('#add-new-submit').on('click', function(){
// Get new option from text field
var value = $('#add-new-text').val();
console.log(value);
$.ajax({
type: "POST",
url: "<?php echo site_url(); ?>main/change_options",
data: {new_option: value, new_option_class: Classofentry},
dataType: "html",
error: errorHandler,
success: success
});
function success(data){
//$('#animal_species').append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
$('#'+Classofentry).append("<option value='" + data + "'selected=\"selected\">" + data + "</option>"); // This might work ...
alert(data);
//alert('Success!');
}
function errorHandler(){
alert('Error with AJAX!');
}
$('#add-new').modal('toggle');
});
});
</script>
我将 $(this) 元素向上移动以访问所选选项。