感谢您的回复!我找到了一种方法,可以通过保留嵌套的点击但不绑定第二个点击来使其正常工作。我无法获得建议的解决方案(使所有功能都嵌套)正常工作。当它们没有嵌套时,似乎没有办法使第二次点击起作用。我不知道为什么。在调用ajax的函数中还必须具有success和errorHandler函数。这是代码(与上面的问题相同,但在第二个嵌套单击中包含unbind语句):
<script type="text/javascript"> var Classofentry = ''; $('#upload_form option[value="addnew"]').click(function(){ // Show modal window $('#add-new').modal('show'); // Get the class var Classofentry = $(this).attr("class"); console.log(Classofentry);Thanks $('#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 }); $('#add-new-submit').unbind('click') // <-------------- The answer!!!!! $('#add-new').modal('toggle'); function success(data) { //$('#animal_species').append("<option value='" + data + "'selected="selected">" + data + "</option>"); $('#'+Classofentry).append("<option value='" + data + "'selected="selected">" + data + "</option>"); //alert(data); //alert('Success!'); } function errorHandler() { alert('Error with AJAX!'); } }); }); </script>


