我将标签设置为按钮,并使用它代替<button>或<input submit>标签.
为了识别操作,我使用href属性,例如:
<a href='update'>Update</a>
<a href='delete'>Delete</a>
Run Code Online (Sandbox Code Playgroud)
然后使用jQuery:$('a [href ="update"]').click(function(){...});
它运作良好.当给定链接充当表单上的提交按钮时,我还使用href属性作为"operation"参数的值.
现在的问题是 - 搜索引擎会因为盲目链接而惩罚我吗?(我认同).
我该如何解决?用#update而代替?我不能使用id属性,因为可能有多个按钮执行相同的操作.
注意:点击处理程序可以使用类连接,这很方便.所以我宁愿不使用id多个按钮连接到同一个处理程序.
<!-- inside a form -->
<a href='update' class='submit-button'>Update</a>
<!-- inside $(function() { .. });
$('a.submit-button').click(function() {
// 1. get href of the clicked link : href = $(this).attr('href');
// 2. add <input hidden> to the form: <input type='hidden' name='operation' value='<href>'/>
// 3. submit form : $(this).parents('form').submit();
});
Run Code Online (Sandbox Code Playgroud)
您可以使用"#update"或"#insert",但您应该使用class属性来区分它们.你能为单个元素设置多个类吗?与ID属性不同,可以在文档中包含多个相同的类.
<a href="javascript:void(0);" class="update_buton submit_button">Update</a>
Run Code Online (Sandbox Code Playgroud)
然后你就可以这样做了:
$('.update_button').click(function(e)
{
// Do something here
});
Run Code Online (Sandbox Code Playgroud)
如果您需要能够区分单击哪种类型的"submit_button",则可以执行以下操作:
$('.submit_button').click(function(e)
{
// Common code up here
if ( $(this).hasClass('.update_button') )
{
// Update here
}
else if ( $(this).hasClass('.other_button') )
{
// Other action here
}
// More common code down here
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1889 次 |
| 最近记录: |