简体   繁体   English

jQuery将隐藏的表单字段发布到ColdFusion查询

[英]jQuery Post Hidden Form Field to ColdFusion Query

I have a jQuery tab element with 2 tabs. 我有一个带有2个标签的jQuery标签元素。 There is a form on each tab that has its own action page respectively. 每个选项卡上都有一个表单,分别具有自己的操作页面。

I also have JavaScript code that prevents the default of page refresh and posts the data to my action-page. 我还有JavaScript代码,可以防止默认的页面刷新并将数据发布到我的操作页面。

The second tab on my form has multiple unique form elements that are created by a CFLoop. 表单上的第二个选项卡具有多个由CFLoop创建的唯一表单元素。 I use this to delete specific records from the database. 我用它来删除数据库中的特定记录。 But when I submit the form, the query on my action page throws an error. 但是,当我提交表单时,操作页面上的查询会引发错误。 I can see the error in Firebug but not on screen (If anyone knows of a way to see the ColdFusion error code in more detail, that would be helpful): 我可以在Firebug中看到错误,但不能在屏幕上看到(如果有人知道更详细地查看ColdFusion错误代码的方法,那将很有帮助):

500 (Error Executing Database Query.) 500(错误执行数据库查询。)

I pass the record ID along to the query on the actionpage in the form of a hidden form field. 我以隐藏表单字段的形式将记录ID传递给操作页上的查询。 Is there a different way that I need to do this with jQuery? 我需要用jQuery执行其他方法吗? Thanks for the help. 谢谢您的帮助。

Form Code: 表格代码:

<cfloop query="get_trips">
  <tr class="vehicle-log-table">
    <td class="vehicle-log-table">#DateFormat(get_trips._date, "mm-dd-yyyy")#</td>
    <td class="vehicle-log-table"><div align="center">#get_trips.total_mileage#</div></td>
    <td class="vehicle-log-table"><div align="center">#get_trips.expenses#</div></td>
    <td class="vehicle-log-table">
      <div align="right"> 
        <form class="deleteMileageForm" id="deletemileage#get_trips.currentRow#" method="post">
          <input class="vehicle-log-form" type="submit" id="submit2" name="submi2" value="Delete">
          <input type="hidden" id="hidden" name="hidden" value="#get_trips.id#">
        </form>           
      </div><br />

      <span class="errorTab2" style="display:none"> <font color="##FF0000"> <strong>Trip Not Deleted</strong></font></span>
      <span class="successTab2" style="display:none">
        <font color="##00FF00"> 
          <strong>Trip Deleted Successfully</strong>
        </font>
      </span>  
    </td>
  </tr>
</cfloop>

jQuery Code: jQuery代码:

<script>
  $(document).ready(function () {
    //Submit form to add record.
    $('#addmileage').submit(function (e) {          
      e.preventDefault();
      $.ajax({
        data: $('#addmileage').serialize(),
        type:'POST',
        url:'actionpages/add_trip.cfm?ticketid=<cfoutput>#url.ticketid#</cfoutput>',
        success: function(){
          $('.success').fadeIn(200).show();
          $('.error').fadeOut(200).hide();
        }
      });
    });


    $('.deleteMileageForm').submit(function (e) {          
      e.preventDefault();
      $.ajax({
        data: $('.deleteMileageForm').serialize(),
        type:'POST',
        url:'actionpages/delete_trip.cfm',
        success: function(){
          $('.successTab2').fadeIn(200).show();
          $('.errorTab2').fadeOut(200).hide();
        }
      });
    });     
  });    
</script>

ColdFusion Query ColdFusion查询

<!---Delete Trip --->                          
<cfoutput>
  <cfquery name="deleteTrips" datasource="#datasource#">
    delete from vehicle_log
    where ID = #form.hidden#
  </cfquery>
</cfoutput>

Can anyone help? 有人可以帮忙吗? much appreciated. 非常感激。

The problem, as Matt commented, is that all the forms on your second tab have the same ID (deletemileage). 正如Matt所评论的那样,问题在于第二个选项卡上的所有表单都具有相同的ID(删除里程)。 So when you try to bind to the submit event handler with 因此,当您尝试使用以下方法绑定到Submit事件处理程序时:

$('#deletemileage').submit(function (e) { ... } );

you're actually only binding the first DOM element that jQuery finds that matches your selector. 您实际上仅绑定jQuery找到的与选择器匹配的第一个DOM元素。 The first DOM element with an ID of deletemileage is your first form on tab2, so it gets bound correctly, and the rest don't get bound at all. ID为deletemileage的第一个DOM元素是您在tab2上的第一个表单,因此它可以正确绑定,而其余元素则完全不绑定。

From the jQuery documentation: 从jQuery文档中:

"Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid." “每个id值必须在文档中仅使用一次。如果为多个元素分配了相同的ID,则使用该ID的查询将只选择DOM中的第一个匹配元素。但是,不应依赖此行为;具有多个使用相同ID的元素的文档无效。”

So, two things. 所以,两件事。 First, as Matt suggested define your ids like id="deletemileage#get_trips.current_row#" to make the HTML valid. 首先,正如马特(Matt)建议的那样,定义您的ID(例如id =“ deletemileage#get_trips.current_row#”)以使HTML有效。 Second if you want to bind the submit event to all of the forms, give them a class like class="deleteMileageForm". 其次,如果您要将Submit事件绑定到所有表单,请给它们一个类,例如class =“ deleteMileageForm”。 Then use 然后使用

$(".deleteMileageForm").submit(function(e) { ... } );

To bind your submit handler. 绑定您的提交处理程序。 Hope that helps. 希望能有所帮助。

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

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