使用jQuery读取动态创建的内容
说到jQuery,我算是个新手,但多亏了另一个stackoverflow成员,我有了一些代码,允许用户单击单选按钮,并让它添加一个表行,其中填充了他们单击的单选按钮链接的文本。以下是jQuery代码:使用jQuery读取动态创建的内容,jquery,Jquery,说到jQuery,我算是个新手,但多亏了另一个stackoverflow成员,我有了一些代码,允许用户单击单选按钮,并让它添加一个表行,其中填充了他们单击的单选按钮链接的文本。以下是jQuery代码: $('.addRow').click(function() { var tr = $( '<tr><td class="cat"></td>' + '<td class="sel"></td>'
$('.addRow').click(function() {
var tr = $(
'<tr><td class="cat"></td>'
+ '<td class="sel"></td>'
+ '<td><textarea name="cmo-paragraph[' + count + ']">Click to edit (id '
+ count
+ ')</textarea></td></tr>'
);
$('.mainTable > tbody:last').one().append(tr);
tr.find(".cat").text($(this).closest("li.category").attr("title"));
tr.find(".sel").text($(this).closest("li.select").attr("title"));
count++;
});
$('.addRow')。单击(函数(){
var tr=$(
''
+ ''
+'单击以编辑(id'
+计数
+ ')'
);
$('.mainTable>tbody:last').one().append(tr);
tr.find(“.cat”).text($(this.closest(“li.category”).attr(“title”));
tr.find(“.sel”).text($(this.closest(“li.select”).attr(“title”));
计数++;
});
以下是广播列表的一个片段:
<li class="category" value="1" title="Chronic Pain Referral">Chronic Pain Referral
<ul>
<li class="select" title="Chronic referral">
<input type="radio" class="addRow" id="radio1" name="modalities" value="1">
<label for="radio1">Chronic referral</label>
</li>
</ul>
</li>
慢性疼痛转诊
-
长期转诊
这将成功地每次向表中添加一行,其中“类别”和“选择”表单元格的前端分别填充有“慢性疼痛转诊”和“慢性转诊”
问题是我希望能够以表单的形式提交这些信息以保存到数据库中。由于“count”变量,我可以遍历行,因此我可以捕获textarea数据,但当前未传递每行的“category”和“select”单元格的值
我尝试了以下代码更改,但似乎不起作用:
$('.addRow').click(function() {
var tr = $(
'<tr><td class="cat"><input type="hidden" class="hiddenCat" name="category[' + count + ']"></td>'
+ '<td class="sel"></td>'
+ '<td><textarea name="paragraph[' + count + ']">Click to edit (id ' + count + ')</textarea></td></tr>'
);
$('.mainTable > tbody:last').one().append(tr);
tr.find(".cat").text($(this).closest("li.category").attr("title"));
tr.find(".hiddenCat").text($(this).closest("li.category").attr("value"));
tr.find(".sel").text($(this).closest("li.select").attr("title"));
count++;
});
$('.addRow')。单击(函数(){
var tr=$(
''
+ ''
+'单击以编辑(id'+计数+')'
);
$('.mainTable>tbody:last').one().append(tr);
tr.find(“.cat”).text($(this.closest(“li.category”).attr(“title”));
tr.find(“.hiddenCat”).text($(this.closest(“li.category”).attr(“value”));
tr.find(“.sel”).text($(this.closest(“li.select”).attr(“title”));
计数++;
});
我似乎没有很好地解释我的情况,但我确实找到了我想要的答案,我将与大家分享
$('.addRow').click(function() {
var tr = $(
'<tr><td class="cat"></td>'
+ '<td class="sel"></td>'
+ '<td><textarea name="paragraph[' + count + ']">Click to edit (id ' + count + ')</textarea></td><td><input type="text" name="catText[' + count + ']" class="hiddenCat"></td></tr>');
$('.mainTable > tbody:last').one().append(tr);
tr.find(".cat").text($(this).closest("li.category").attr("title"));
tr.find(".hiddenCat").val($(this).closest("li.category").attr("title"));
tr.find(".sel").text($(this).closest("li.select").attr("title"));
count++;
});
$('.addRow')。单击(函数(){
var tr=$(
''
+ ''
+'单击以编辑(id'+计数+')';
$('.mainTable>tbody:last').one().append(tr);
tr.find(“.cat”).text($(this.closest(“li.category”).attr(“title”));
tr.find(“.hiddenCat”).val($(this).closest(“li.category”).attr(“title”);
tr.find(“.sel”).text($(this.closest(“li.select”).attr(“title”));
计数++;
});
我不得不添加另一个表格单元格放入隐藏的文本框,因为它在我最初放置的单元格中被覆盖。现在,当用户单击“慢性疼痛转诊”选项时,一个隐藏的文本框将捕获该信息,以便我可以在表单中传递该信息。不清楚问题出在哪里。我看到您使用find()捕获了很多东西,但在这段代码中您没有使用它们。也许试着简化你的问题,只显示相关的代码。你的问题到底是什么?你试过调试你的代码吗?你有什么错误?很抱歉不够清晰,也许一张图片会有帮助。请看这里:现在,当用户单击单选按钮时,左侧无序列表中的“类别”和“选择”值会添加到“组”和“项目”下的表中,但这纯粹是装饰性的。我想做的是在一个隐藏的表单字段中传递这些值,这样我就可以将它们的值提交到数据库。您是否尝试过使用
.val(xpto)
而不是.text(xpto)
设置新的“组”和“项”值?必须传递.val
。检查该元素,如果仅设置.text()
,您将看到其值为空。