简体   繁体   English

如何使用jQuery调用已定义的变量?

[英]How do I call a defined variable with jQuery?

I'm kinda stuck. 我有点卡住了。

I defined three variables: 我定义了三个变量:

var part1 = "<form><input type='button' value='Open Window' onclick='window.open('http://jsfiddle.net/
";
var part2="5Xket,"
var part3 = "'toolbar=no',menubar='no')></form>";

My aim is to concat those variables to create a working link when clicking on the button. 我的目的是在单击按钮时合并这些变量以创建工作链接。

This is my try to concat the values of the variables. 这是我尝试连接变量的值。

var mytest_2=part1+part2+part3;
alert (mytest_2);

By clicking a button the button from mytest2 should appear. 通过单击按钮,应显示mytest2中的按钮。 Clicking on that button there should be opened a new window with the url http://jsfiddle.net/5Xket/ 单击该按钮,应该打开一个新窗口,其URL为http://jsfiddle.net/5Xket/

$('#searchbutton').click(function() {
$("<td class='testclass'><input id='an_id' type='text'></td>").show();
$(mytest_2).insertAfter('#an_id');

Well, the button appears as it should, but won't open a window. 好吧,该按钮应该出现,但不会打开一个窗口。 My guess is that I'm wrong with the syntax somewhere because the alert puts out the correct order of variables. 我的猜测是我在某处使用了错误的语法,因为警报发出了正确的变量顺序。

Any ideas? 有任何想法吗? Thank you. 谢谢。

I should do this way: 我应该这样:

var part1 = '<form><input id="button1" type="button" value="Open Window" /></form>';

$(part1).insertAfter('#an_id');

$('#button1').click(function(){
   window.open('http://jsfiddle.net/','5Xket','toolbar=no,menubar=no');
});

The problem with your code is double/single quotes aren't properly escaped. 您的代码存在的问题是双引号/单引号未正确转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM