gpt4 book ai didi

javascript - 如何在 div 中找到第一个 id

转载 作者:行者123 更新时间:2023-11-30 10:16:37 26 4
gpt4 key购买 nike

我正在尝试创建需要此表单中的 ID 的脚本。我想在 otf_itm 类中找到第一个 id。所以在这种情况下,我正在寻找要返回的 town_id。我尝试了几种不同的选择,但到目前为止,它们都未定义

$('.otf_itm:first-child').attr('id');
$('.otf_itm').find('id');
$('.otf_itm').find().attr('id');

<div class="otf_itm">
{if $towns|@count gt 0}
<select class="form-control chosen add_town" name="town_id" id="town_id" data-rule-required="true" data-placeholder="Choose a town">
<option value=""></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>
{else}
<p class="mt5">You have no towns configured.</p>
<p class="mt5"><a href="#modal-1" role="button" id="town_id" class="showhouse-text" data-toggle="modal">Add new town</a></p>
{/if}
</div>

最佳答案

我建议:

// selects elements with an id attribute within the .otf_itm element:
$('.otf_itm [id]')
// retrieves the first:
.first()
// gets the id property from that jQuery object:
.prop('id');

或者:

// selects elements with an id attribute within the .otf_itm element:
$('.otf_itm [id]')
// retrieves the first:
.eq(0)
// gets the id property from that jQuery object:
.prop('id');

引用资料:

关于javascript - 如何在 div 中找到第一个 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23343608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com