如何在加载dom后与添加的id交互
How to interact with an added id after dom loaded
我有一个页面,根据服务器端的数据,它将有一个选择框或一个链接。如果选择框可用,则我可以获得id(在本例中为town_id(。但是,如果没有选择框,则我将创建一个,但我使用输入分类otf_form_field_id 中的值设置选择id和名称
<div class="otf_form_field">
<input type="hidden" class="otf_form_field_name" value="town">
<input type="hidden" class="otf_form_field_id" value="town_id">
<!-- select will be hidden with templating -->
<!-- <select class="form-control chosen" name="town_id" id="town_id" data-rule-required="true" data-placeholder="Choose a town">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
{foreach $towns as $town}
<option value="{$town.id}" {if $town.id eq $form_data.town_id}selected="selected"{/if}>{$town.town_name}{if $town.region_name}, {$town.region_name}{/if}</option>
{/foreach}
<option value="ADD_NEW">Add new town...</option>
</select> -->
<p class="mt5">You have no towns configured.</p>
<p class="mt5"><a href="#modal-1" role="button" class="showhouse-text" data-toggle="modal">Add new town</a></p>
</div>
if($('#' + select_id).length === 0){
行中的以下代码检查是否存在选择框。如果没有,我需要创建一个。我可以创建它,但当我从下拉菜单中选择addnew时,模态不会显示。我用town_id = make_select_id
将select_id更改为(在本例中(town_id,并且我认为使用.on事件会捕获在加载dom $('.otf_form_field').on('change', '#'+select_id, function(){
之后添加的项目
如果有一个填充了选项的选择框,并且我有添加新选项,它将显示正常。
// get the field name, will be set in hidden input
var otf_form_field_name = $('.otf_form_field_name').val();
// get the id of the select box
var select_id = $('.otf_form_field select').prop('id');
// if there is no select box get value to assign it later
if(select_id === undefined){
var make_select_id = $('.otf_form_field_id').val();
}
// set the option text to show on the chosen search box if there
// are no results being returned
$("#" + select_id).chosen({no_results_text: '<a href="#modal-1" role="button" class="showhouse-text" data-toggle="modal">Add new ' + otf_form_field_name + '...</a> - No results match', disable_search_threshold: 15});
// hide the modal notificaiton and checking gif
$('#saving_otf_gif, .modal_notification').hide();
// when the chosen (which will use on the fly) is changed
$('.otf_form_field').on('change', '#'+select_id, function(){
// check if the value is equal to 'ADD_NEW'
if($(this).val() == "ADD_NEW"){
// show the modal
}
});
// when the save button on the modal is clicked
$('#save_otf_btn').click(function(e){
// if the form is valid
if($('#validation-form-two').valid()){
// e.preventDefault() stops the server side redirecting
e.preventDefault();
// hide the save button so cannot multiple click
// show the buffer gif
$('#save_otf_btn').hide();
$('#saving_otf_gif').show();
// check to see if there was an id, if there there were no items available
// then there would be no select box and we need none
if($('#' + select_id).length === 0){
// build the select box with item_id and item_name
$('.otf_form_field').html('<select class="form-control chosen" name="' + make_select_id + '" id="' + make_select_id + '" data-rule-required="true" data-placeholder="Choose ' + otf_form_field_name + '..."></select');
// make it chosen with css hack
$("#" + make_select_id).chosen({disable_search_threshold: 15});
$("#" + make_select_id).css('width', '100%');
// append new option into select box
$('#' + make_select_id).append('<option value="TEST" selected>TEST</option>');
// move the ADD_NEW option to bottom of list and update all
$('#' + make_select_id).append('<option value="ADD_NEW">Add new ' + otf_form_field_name + '...</option>');
$('#' + make_select_id).trigger('chosen:updated');
select_id = $('.otf_form_field select').prop('id');
} else {
// just append option, no need to create select
}
// hide the modal and reset button incase user
}
});
当DOM加载并且select不存在时,您的代码会附加onchange
事件,但没有select
元素可以附加它。
创建select并将其添加到DOM后,再次将onchange
事件附加到select。
$('.otf_form_field').html('<select class="form-control chosen" name="' + make_select_id + '" id="' + make_select_id + '" data-rule-required="true" data-placeholder="Choose ' + otf_form_field_name + '..."></select');
$('.otf_form_field').on('change', '#'+select_id, function(){
// check if the value is equal to 'ADD_NEW'
if($(this).val() == "ADD_NEW"){
// show the modal
}
});
不过,我会把它放在一个函数中,并调用它,因为您在加载时调用它,并且当您手动将select添加到DOM时:
function addOnChangeHandler(){
$('.otf_form_field').on('change', '#'+select_id, function(){
// check if the value is equal to 'ADD_NEW'
if($(this).val() == "ADD_NEW"){
// show the modal
}
});
}
然后
$('.otf_form_field').html('<select class="form-control chosen" name="' + make_select_id + '" id="' + make_select_id + '" data-rule-required="true" data-placeholder="Choose ' + otf_form_field_name + '..."></select');
addOnChangeHandler();
只需在页面加载时调用相同的函数。
(这不仅仅是一个答案(
jQuery的on
的一个真正伟大的功能是侦听与传递给它的选择器匹配的新元素。当你必须等到知道元素的id时,这并没有多大帮助,但如果你知道这些新的<select>
将有一个特定的类,你可以重写为$('.otf_form_field').on('change', '.select-classname', function(){ ... });
,而不用担心时间问题。
- 动态添加id's到输入字段
- 通过Couchdb中的更新处理程序添加id和author字段
- 如何在CKEditor中为图像添加ID
- 添加ID's在一个P HTML标签中的一个翡翠模板中
- 向Fabric.js元素添加ID
- 如何添加id和额外的参数到一个放大弹出iframe与youtube视频,所以我可以控制与API youtube播放器
- 创建折叠选项卡并动态添加 ID 和指向 jQuery 中 ID 锚点的链接
- 使用 for 循环,如何迭代对象以向其添加 id 属性
- 在“挖空”中添加 ID
- 避免在向标记添加 ID 时触发 DOMSubtreeModified 事件
- 我无法在 foreach 中向对象添加 ID
- 使用主干视图在模板上动态添加 ID 和类
- 在Angular对象中循环时向HTML添加id
- 为Datatable中的行添加id
- 使用jQuery向子级添加id
- 如何仅在li元素存在的情况下向pre元素添加id
- 如何使用javascript从最近的li元素向pre-tag添加id
- 如何在javascript中向输入字段添加ID
- jQuery或Javascript单击函数更改或添加id
- 将jquery变量连接到字符串,以便添加id's转换为动态生成的输入字段