[英]How Can I concatenate a parameter string in javascript function on string in appended html?
This is my code for html这是我的 html 代码
<div id="content"></div>
Then I append an inpunt to #content:然后我将一个输入附加到#content:
$( document ).ready(function() {
// Handler for .ready() called.
var parameter = "<p>Hola</p>";
$("#content").append('<div><p>click the button</p>'+
'<input type="submit" name="submit_answers" value="Submit" onclick="getValue();" >'+
'<input type="submit" name="submit_answers" value="Submit" onclick="'+getValue2(parameter)+'" >'+
'</div>');
});
function getValue2(parameter){
alert(parameter);
}
function getValue(){
alert("Hola");
}
The first input works very well, but the second input dosen´t work after document is ready.第一个输入效果很好,但文档准备好后第二个输入不起作用。 What´s the better way to declare a function in this case?
在这种情况下声明函数的更好方法是什么?
你可以试试这个:
'<input type="submit" name="submit_answers" value="Submit" onclick="getValue2(\'' + parameter + '\');" >'
You could do this:你可以这样做:
onclick="getValue2("' + parameter + '")"
But something like this would be better:但这样的事情会更好:
var $div = $('<div><p>click the botton</p></div>');
var $button = $('<input type="submit" name="submit_answers" value="Submit">')
.data('parameter', parameter)
.click(function () {
getValue2($(this).data('parameter'));
}).appendTo($div);
$("#content").append($div);
I think you probably just weren't separating correctly.我想你可能只是没有正确分离。
Try this example :试试这个例子:
onclick= "mySup(\''+json.id+'\',\''+json.description+'\');"
Try this :尝试这个 :
$( document ).ready(function() {
// Handler for .ready() called.
var parameter = "<p>Hola new</p>";
$("#content").append('<div><p>click the botton</p>'+
'<input type="submit" name="submit_answers" value="Submit" onclick="getValue();" >'+
'<input type="submit" name="submit_answers" value="Submit" onclick="getValue2(\''+parameter+'\');" >'+
'</div>');
});
第二个输入应该是这样的。
'<input type="submit" name="submit_answers" value="Submit" onclick="getValue2('+parameter+')" >'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.