简体   繁体   English

将参数传递给JQuery onclick事件

[英]Passing Parameters to JQuery onclick event

I am new in Jquery, and need the correct strategy to carry out a dynamic form. 我是Jquery的新手,需要正确的策略来执行动态表单。

I want to create a dynamic form. 我想创建一个动态表单。 I retrieve values from database, display them as rows & then for every value, the user can add as many rows as he want. 我从数据库中检索值,将它们显示为行然后对于每个值,用户可以根据需要添加任意数量的行。 below code show a loop that create a table for every value, with "customFields" ID, which is append with "$i" variable to make it unique. 下面的代码显示了一个为每个值创建一个表的循环,带有“customFields”ID,后面带有“$ i”变量以使其唯一。

// query code
$i = 1;
 while ($row = mysql_fetch_array($sqlQ))
  {
         $var1 = $row['sid'];
         $var2 = $total_rows;
 ?>
     <table id="customFields<?php echo $i; ?>" class="box-table-a" align="left">

<thead>
    <tr>
       <th colspan="5" scope="col"><?php echo $row['VS_NAME']; ?></th>
       <th  scope="col" align="Right"><a href="javascript:void(0)" id="addCF<?php echo $i++; ?>"     >Add Row</a></th>
   </tr>
  </thead>

</table>

 }

Now for this code, I write the following javascript Append code. 现在为此代码,我编写以下javascript附加代码。

 <script>
 <?php for ($i = 1; $i <=5; $i++) { ?>
     $("#addCF"+<?php echo $i; ?>).click(function(){
         $("#customFields<?php echo $i;?>").append('<tr ><td width="13%"><input type="text" name="godkant[]" class="fieldWidth" /></td><td width="11%" style="background:#b5dbe6" ><input type="text" name="foreRengoring[]" class="fieldWidth" /></td><td  width="12%" style="background:#e6b8b8"  ><input type="text" name="efterRengoring[]" class="fieldWidth" /></td><td width="12%" style="background:#c0d498" ><input type="text" name="borvarden[]" class="fieldWidth" /></td><td width="12%" style="background:#ffff66" ><input type="text" name="injust[]" class="fieldWidth" /></td><td width="40%" ><input type="text" name="noteringar[]" class="fieldWidthNote" /></td></tr>'); });
    <?php } ?>
 </script>

The above code works perfectly fine. 上面的代码完全正常。 but I don't know the number of values my php while loop will return. 但我不知道我的php while循环将返回的值的数量。 so i need to pass two values to this javascript click event. 所以我需要将两个值传递给此javascript点击事件。 one for the loop, and 2nd for to be displayed when we add a row. 一个用于循环,第二个用于在我们添加行时显示。

Questions; 问题; 1. what is the best strategy to carry out this kind of function. 1.实现这种功能的最佳策略是什么? 2. can I use Jquery event handler (if i am calling it correctly - $(#id).append...) in my own function which can be called on Onclick event? 2.我可以使用Jquery事件处理程序(如果我正确地调用它 - $(#id).append ...)在我自己的函数中可以在Onclick事件上调用吗?

I hope that I explain the problem correctly. 我希望我能正确解释这个问题。 This question is asked many times, but I am new to Jquery, thats why i am not able to map the answers to my solution. 这个问题被多次询问,但我是Jquery的新手,这就是为什么我无法将答案映射到我的解决方案。

need help. 需要帮忙。

Thanks 谢谢

You don't need PHP inside your jQuery in this case. 在这种情况下,您的jQuery中不需要PHP。 You can refactor it like this: 你可以像这样重构它:

$('.box-table-a a').click(function (evt) {
    evt.preventDefault();
    $(this).parents('table').append(rowHtml);
    return false;
});

where rowHtml is the HTML you want to add. 其中rowHtml是您要添加的HTML。 If you plan on having more than one link per table, you should assign a class to your add link (eg, add-link ), then your event listener becomes $('.add-link').click . 如果您计划每个表有多个链接,则应该为添加链接分配一个类(例如, add-link ),然后您的事件监听器变为$('.add-link').click You should also replace <a href="javascript:void(0)" in your HTML with <a href="#" . 您还应该使用<a href="#"替换HTML中的<a href="javascript:void(0)" <a href="#"

Fiddle: http://jsfiddle.net/verashn/7HCZu/ 小提琴: http//jsfiddle.net/verashn/7HCZu/

EDIT 编辑

To pass additional data to your rows, place the data in your table element like this: 要将其他数据传递到您的行,请将数据放在table元素中,如下所示:

<table ... data-rowid="<?php echo $var1; ?>" data-total="<?php echo $var2; ?>">

Then read it with jQuery: 然后用jQuery读取它:

$('.box-table-a a').click(function (evt) {
    ...
    var rowId = $(this).parents('table').data('rowid');
    var total = $(this).parents('table').data('total');

});

Demo (this puts ID & total into the 1st and 2nd input field of every row): http://jsfiddle.net/verashn/7HCZu/5/ 演示(这会将ID和总数放入每行的第一个和第二个输入字段): http//jsfiddle.net/verashn/7HCZu/5/

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

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