2013-03-24 93 views
0

我有两个不同的按钮。这两种方式在点击时运行JQuery函数'ShowCommentBox' 但是,当点击第二个按钮时,我想沿'SHowCommentBox'加载额外的JQuery函数 - 附加功能允许在屏幕上显示额外的选项。2个按钮之一有额外的功能

<input id="SubmitCommentsForBOQ" type="button" value="Comments" onclick="ShowCommentBox('<%: item.ItemCode %>'')" /> 

以上是第二个按钮,我想也运行

$("#SubmitCommentsForTwo").click(function() { 
     $("#hiddenBox").show(); 
    }); 

,这使得可见的额外功能...我怎样才能做到这一点?

谢谢你的任何答复

以下是原始JQuery的:它加载一个对话框

function ShowCommentBox(itemIdentifier, labourOrPlant, desc) { 
     id = itemIdentifier; 
     LabouringOrPlanting = labourOrPlant; 
     description = desc; 
     Function.DisplayBox(itemIdentifier); 
     $("#LabourOrPlantDialog").dialog({ modal: true }); 
    } 

和我的其他代码:

<div id="LabourOrPlantDialog" title="Comments" style="display:none;"> 
<table class="detailstable FadeOutOnEdit"> 
    <tr> 
     <th>Item</th> 
    </tr> 
    <tr> 
     <td id="Item"></td> 
    </tr> 
</table>  
<br /> 

     <textarea id="ExistingComments" type="text" runat="server" rows="7" cols="30" 
     maxlength="2000"> </textarea> 
     <input id="SubmitComment" type="button" value="Submit" 
      onclick="SubmitButton()" /> 

<br /> 

<div id="hiddenBox"> 
<input type="text" name="BoqTextBox" id="BoqTextBox" value="7.15" /> 
</div> 
</div> 
+0

如果您有两个不同的按钮,为什么不只是更改第二个按钮的onclick函数?他们是两个单独的DOM元素,对吗? – antinescience 2013-03-24 21:11:14

回答

1

这是最好的separate behavior from markup。您可以使用HTML data-属性解决这两个问题。

首先嵌入在HTML中的数据:

<input id="SubmitCommentsForBOQ" type="button" value="Comments" 
    data-item-code="<%: item.ItemCode %>" /> 

相反的onclick,仅使用jQuery绑定事件处理程序,并执行所有你需要在一次行动:

$("#SubmitCommentsForBOQ").click(function() { 
    var itemCode = $(this).data('itemCode'); 
    ShowCommentBox(itemCode); 
}); 

$("#SubmitCommentsForTwo").click(function() { 
    $("#hiddenBox").show(); 
    var itemCode = $(this).data('itemCode'); 
    ShowCommentBox(itemCode); 
}); 

多个处理程序将按照它们绑定的顺序执行,所以你也可以这样做:

// happens first, but only for this specific button 
$("#SubmitCommentsForTwo").click(function() { 
    $("#hiddenBox").show(); 
}); 

// happens for all buttons 
$("input[data-item-code]").click(function() { 
    var itemCode = $(this).data('itemCode'); 
    ShowCommentBox(itemCode); 
}); 
相关问题