我如何连接javascript函数中的字符串中的参数字符串在附加的HTML? [英] How Can I concatenate a parameter string in javascript function on string in appended html?

查看:93
本文介绍了我如何连接javascript函数中的字符串中的参数字符串在附加的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码html

 < div id =content>< / div> 

然后我将一个inpunt追加到#content:




var parameter =< p> Hola< ; / p>;
$(#content)。append('< div>< p>点击按钮< / p>'+
'< input type =submit name =submit_answersvalue =Submitonclick =getValue();>'+
'< input type =submitname =submit_answersvalue =Submitonclick =' + getValue2(parameter)+'>'+
'< / div>');
});

函数getValue2(参数){
alert(parameter);


函数getValue(){
alert(Hola);
}

第一个输入工作得很好,但第二个输入不工作文件已准备就绪。在这种情况下声明函数的更好方法是什么?

解决方案

您可以这样做:

  onclick =getValue2('+ parameter +')

但是像这样会更好:

  var $ div = $(' < div>< p>按一下[botton< / p>< / div>'); 
var $ button = $('< input type =submitname =submit_answersvalue =Submit>')
.data('parameter',parameter)
.click(function(){
getValue2($(this).data('parameter'));
})。appendTo($ div);

$(#content)。append($ div);


This is my code for html

 <div id="content"></div> 

Then I append an inpunt to #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?

解决方案

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);

这篇关于我如何连接javascript函数中的字符串中的参数字符串在附加的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆