简体   繁体   English

如何使用jquery将动态变量附加到div?

[英]How can I append dynamic variables to div with jquery?

I'm having some difficulty getting this to work. 我在使它起作用时遇到了一些困难。 What I want to do is take dynamic variables from the onclick and place them into a div I am appending to another div in the document. 我想做的是从onclick中获取动态变量,并将其放入我要附加到文档中另一个div的div中。 Since each each item will have variables associated from a database query, I figured populating the buildTicket() variables from the database would be easier. 由于每个项目都会有一个与数据库查询关联的变量,因此我认为从数据库中填充buildTicket()变量会更容易。

I know I'm doing something wrong. 我知道我做错了。 I just can't figure out what. 我只是不知道是什么。

If you have a better way, I'm all ears. 如果您有更好的方法,我会全力以赴。

Here is my javascript: 这是我的JavaScript:

<script type="text/javascript">
$(document).ready(function(){
$(function() {
buildTicket = function(eventname,ticketprice,venueid,userid) {
    $(".ticketbtn").click(function(){
      $(".ticketwindow").append("<div class='ticket'>" + eventname + " - " +     eventprice + "</div>");
    });
}
});
</script>

Here is my HTML: 这是我的HTML:

<div class="ticketbtn" onclick="buildTicket('Some Show','12.00','1','2');">
        <img src="assets/images/tixicon.png" alt=""> <div class="eventname">Some Show</div>
        <div class="ticketprice">Adult - 12.00    </div>
    </div>

<div id="ticketwindow">

</div>

Can someone help me figure this out? 有人可以帮我解决这个问题吗?

(sorry for the code formatting. Still trying to figure out how to use stackoverflow's forms properly.) (很抱歉,代码格式。仍然试图弄清楚如何正确使用stackoverflow的形式。)

Thanks, Joe 谢谢乔

You are binding the onClick listener on click of your button. 您将在单击按钮时绑定onClick侦听器。

If you must use the onClick element attribute then the following code will work although you should become familiar with the principles of Unobtrusive JavaScript . 如果您必须使用onClick元素属性,那么尽管您应该熟悉Unobtrusive JavaScript的原理,但是下面的代码将起作用。

$(document).ready(function(){
    var buildTicket = function(eventname,ticketprice,venueid,userid) {
        $("#ticketwindow").append("<div class='ticket'>" + eventname + " - " + eventprice + "</div>");
    });
});

Firstly, $(function() { is equivalent to $(document).ready(function() { so you only need one of them. 首先, $(function() {等效于$(document).ready(function() {因此,您只需要其中一个。

Secondly, you don't need to use the onclick attribute if you are binding to click() with jQuery, or vice versa. 其次,如果要通过jQuery绑定到click() ,则不需要使用onclick属性,反之亦然。

If you just use the onclick attribute, then you can remove your document ready handler, and your click() binding, all together and simply define the buildTicket() function. 如果仅使用onclick属性,则可以一起删除文档就绪处理程序和click()绑定,只需定义buildTicket()函数即可。

Thirdly, the eventprice variable is misnamed in buildTicket() . 第三, eventprice变量在buildTicket()buildTicket()命名。

Here is a working fiddle, using jQuery to bind to the click event of your button div. 这是一个有效的小提琴,使用jQuery绑定到按钮div的click事件。 http://jsfiddle.net/BdJAL/ http://jsfiddle.net/BdJAL/

I quote from your question: "I figured populating the buildTicket() variables from the database would be easier." 我引用您的问题:“我认为从数据库中填充buildTicket()变量会更容易。”

If you want to get hold of database data in your JavaScript how about making an HTTP request to get it. 如果要在JavaScript中保留数据库数据,如何发出HTTP请求来获取它。 Obviously, you could do this in the normal XJAX way using XMLHttpRequest: http://www.w3schools.com/xml/xml_http.asp Or you could call up some server side code to produce JSON for you and reference it in a <script> tag eg <script type="text/javascript" src="../js/jsonFromServerSideCode.jsp"></script> 显然,您可以使用XMLHttpRequest以常规XJAX方式执行此操作: http : //www.w3schools.com/xml/xml_http.asp或者您可以调用一些服务器端代码来为您生成JSON并在<script中引用它>标记,例如<script type =“ text / javascript” src =“ ../ js / jsonFromServerSideCode.jsp”> </ script>

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

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