选择一个动态生成的字符串。

huangapple go评论60阅读模式
英文:

Selecting a dynamically generated string

问题

On button click, I want to send data to the server for a certain line. Rows in the table are created dynamically

There is the following code:

<tr class="ordertr table-bordered">
<td id="recid" class="d-none"><?=$row['RecId']?></td>
<td class="ps-lg-3">
<ul>
<li><button id="editingST">&#9998;</button></li>
<li><button class="towork">&#128736;</button></li>
</ul>
</td>
</tr>

And JS:

$("#position-order tbody").on('click', '.towork', function gotowork(){
 $.ajax({
type: "POST",
url: '/towork.php',
data: {recid: $('#recid').text()}
});
return false;
});

recid is defined, but for all rows it takes the value from the first row

I think there is an error here - $("#position-order tbody")

英文:

On button click, I want to send data to the server for a certain line. Rows in the table are created dynamically

There is the following code:

&lt;tr class=&quot;ordertr table-bordered&quot;&gt;
&lt;td id=&quot;recid&quot; class=&quot;d-none&quot;&gt;&lt;?=$row[&#39;RecId&#39;]?&gt;&lt;/td&gt;
&lt;td class=&quot;ps-lg-3&quot;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;button id=&quot;editingST&quot;&gt;&amp;#9998;&lt;/button&gt;&lt;/li&gt;
&lt;li&gt;&lt;button class=&quot;towork&quot;&gt;&#128736;&lt;/button&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;

And JS:

$(&quot;#position-order tbody&quot;).on(&#39;click&#39;, &#39;.towork&#39;, function gotowork(){
 $.ajax({
type: &quot;POST&quot;,
url: &#39;/towork.php&#39;,
data: {recid: $(&#39;#recid&#39;).text()}
});
return false;
});

recid is defined, but for all rows it takes the value from the first row

I think there is an error here - $(&quot;#position-order tbody&quot;)

答案1

得分: 0

你可以获取包含按钮的行,然后查询该特定的 td。

function gotowork(e) {
    const recid = $(e.target).closest('tr').find('#recid').text();
    // 进行请求...
}

但是,在文档中,id 应该是唯一的,所以你应该使用类(class)来代替。

<td class="recid d-none">文本...</td>
$(e.target).closest('tr').find('.recid').text()
英文:

You can get the row containing the button, then query for that specific td.

function gotowork(e) {
    const recid = $(e.target).closest(&#39;tr&#39;).find(&#39;#recid&#39;).text();
    // make request...
}

However, ids should be unique in a document, so you should use a class for this instead.

&lt;td class=&quot;recid d-none&quot;&gt;text...&lt;/td&gt;
$(e.target).closest(&#39;tr&#39;).find(&#39;.recid&#39;).text()

huangapple
  • 本文由 发表于 2023年5月17日 07:06:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定