[英]Wordpress how to retrieve the post id once it's been reset in the page
[英]How to retrieve id's
我有一個php代碼,其中顯示了來自AZ的按鈕:
<table id="optionAndAnswer" class="optionAndAnswer">
<tr class="answer">
<td>3. Answer</td>
<td>
<?php
$a = range("A","Z");
?>
<table id="answerSection">
<tr>
<?php
$i = 1;
foreach($a as $key => $val){
if($i%7 == 1) echo"<tr><td>";
echo"<input type=\"button\" onclick=\"btnclick(this);\" value=\"$val\" id=\"answer".$val."\" name=\"answer".$val."Name\" class=\"answerBtns answers answerBtnsOff\">";
if($i%7 == 0) echo"</td></tr>";
$i++;
}
?>
...
我想要做的是在jquery中為上述代碼創建一個模板,以便如果用戶要將這些按鈕添加到塊中,則可以使用jquery將這些按鈕添加到塊中,就像模板一樣。
下面是jquery代碼:
var $this, i=0, $row, $cell;
$('#optionAndAnswer .answers').each(function() {
$this = $(this);
if(i%7 == 0) {
$row = $("<tr/>").appendTo($answer);
$cell = $("<td/>").appendTo($row);
}
var $newBtn = $("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this);' />".replace('%s',$this.is(':visible')?'inline-block':'none')).attr('name', $this.attr('name')).attr('value', $this.val()).attr('class', $this.attr('class'));
$newBtn.appendTo($cell);
i++;
});
現在可以正常工作,但是我有一個問題。 如果您查看php代碼,則每個按鈕都有自己的ID,在我的jquery代碼中,每個按鈕都沒有自己的ID。 在我的jquery代碼中,我希望每個按鈕都具有與php代碼中相應按鈕相同的id,唯一不同的是,我希望jquery按鈕的ID以“行”結尾。 例如,如果php中的按鈕F的ID是#answerF
,那么我希望jquery中的按鈕F具有ID #answerFRow
。
我的問題是這可能嗎? 如果我將其放在jquery代碼中,此代碼是否能夠從php代碼中檢索按鈕ID:
.attr('id', $this.attr('id'))
以及如何在結尾處插入“行”一詞?
.attr('id', $this.attr('id')+'Row')
PHP只是生成文本,HTML,瀏覽器會讀取然后呈現。 因此,如果PHP代碼編寫了ID,則jQuery可以讀取它們。
最好的方法是從php對象創建一個javascript數組對象,然后使用此數組對象檢索id
lang=new Object();
<? foreach($a as $key=>$value){ ?>
lang[<?=$key?>] ='<?=$value?>'
<? } ?>
現在,您可以使用此Java腳本對象“ lang”來獲取keyValues。
Java腳本使用+運算符連接字符串, str='over'+'flow';
PHP用。 操作符$str='over'.'flow';
因此,在您的jquery中,它可以是: .attr('id', this.id+'ROW')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.