2015-12-08 35 views
-2

我想一個uniqid()綁定到onclick事件中的字符串:如何插入uniqid()在JavaScript

$UniquepreID = uniqid(); 
$selectbutton = '<input type="button" value="testabc" 
     onclick="selectElementContents(document.getElementById("$UniquepreID"));" />'; 

$UniquepreID字面解析。我怎樣才能使這項工作正確?

回答

1

你的$UniquepreID是在單引號內,所以不會擴大。 See here

試試這個:

$selectbutton = '<input type="button" value="testabc" onclick="selectElementContents(document.getElementById("' . $UniquepreID . '"));" />'; 
+0

謝謝!它現在有效 – nuet

1

你是使用單引號來定義字符串變量$selectbutton。 你要麼使用雙引號,要麼像你這樣使用字符串:

$selectbutton = 'some string with "quotes"' . $uniquepreID . 'more "quotes" here';