gpt4 book ai didi

jquery - 使用 jQuery 从标签中获取文本

转载 作者:行者123 更新时间:2023-12-01 07:45:53 25 4
gpt4 key购买 nike

代码

var $eContainer = $('<div/>', { id: "eContainer", class: 'eList' }).appendTo($injectTo);
for (var i in navProps) {
var np = navProps[i];
var npName = np.name;
var npId = containerId + "_" + npName;

var $eLabel = $('<label />', { 'for': npId, text: npName }).appendTo($eContainer);
$('<input />', { type: 'checkbox', id: npId, value: npName }).prependTo($eLabel);
};

输出

<label for="e_22">
<input type="checkbox" id="selectcolumn_16" value="22">Supplier<div id="eContainer" class="eList">
<label for="a">
<input type="checkbox" id="a" value="A">AAA</label>
<label for="b">
<input type="checkbox" id="b" value="B">BBB</label>
</div>
</label>

问题

  • 它没有关闭输入标签
  • 当我尝试获取标签文本时,它也会返回所有子标签文本

第二个问题的代码

// Event handler for adding labels.
this.$eList.on('click', ':input', $.proxy(function (event) {
var $e = $(event.target);
var selectedLabel = $e.parent().text();
// here it should return "Supplier" when I click on it, but it returns
// SupplierAAABBB -- which is WRONG

编辑

请不要提供带有硬代码的解决方案,因为我需要让它动态工作,所以它必须需要从父级获取它,这些div和控件会有多个层次结构

最佳答案

您可以使用此代码从标签获取文本。

var lbltxt = $('#labelid').text();

关于jquery - 使用 jQuery 从标签中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38139058/

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