gpt4 book ai didi

javascript - 替代用于 chrome 和 IE 8 的 explicitOriginalTarget.id

转载 作者:数据小太阳 更新时间:2023-10-29 05:27:14 27 4
gpt4 key购买 nike

我试图找到一种跨浏览器兼容的方式来挑选在具有两个不同提交按钮的表单提交期间单击的按钮的 id 属性。我能够通过以下方式为 FireFox 完成此操作,但它在 IE 8 或 Chrome 中不起作用,因为它们不支持 explicitOriginalTarget。

   $("#postForm, #dialogPostForm, #pastPostForm").live('submit', function(event) {
event.preventDefault();
if (event.originalEvent.explicitOriginalTarget.id === 'pastPosts'){
...SNIP...

有人可以建议一种跨浏览器兼容的方式来做到这一点吗?

更新我试过使用来自 How can I get the button that caused the submit from the form submit event? 的答案,但我想使用 .live jquery 方法并使用事件对象来选择原始输入提交 ID。

更新这是我试图从中获取原始提交按钮 ID 的表单:

<form id="postForm" action="{% url account_tracker mashup.pk %}?fuzzy=true" method="post">
<div class="mygoal">Update Your Progress: {{ form.post }}
<input type="submit" value="+ 1" class="formbtn" style="font-size:18px;"/>
<input type="submit" value="This week" style="font-size:18px;" id="pastPosts" class="formbtn"/>
<span id="result" class="results"></span>
</div>
</form>

更新我想出了自己的解决方案。添加一个 onclick 属性,为表单添加一个“name”属性。然后在表单处理中检查表单名称是否为“过去”并对其进行处理,然后在表单处理完成后删除该属性。据我所知,这适用于所有浏览器。

 <input type="submit" value="This week" style="font-size:18px;" id="pastPosts" class="formbtn" onclick="$('#postForm').attr('name', 'past');" />

最佳答案

别无选择。

这是 Firefox 独有的属性。

这是一个非常古老的问题,需要回答。

我用 Google 搜索了这个问题并在 http://www.webmuse.co.uk/blog/using-forms-with-multiple-submit-buttons-and-jquery-events/ 上找到了解决方案

基本上,尝试获取焦点元素。

一个例子:

(function($){
$(document).ready(function(){

$('#user_form').submit(function(event){

event.preventDefault(); //stops submission

$('#name_submit')
.text(
//searches all the submit buttons
$('input[type="submit"], button[type="submit"]',this)
.filter(':focus') //finds the focused one
.attr('name') //takes the name
);

});

});
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

<form action="#" id="user_form">
<input type="email" placeholder="Email"/><br>
<input type="password" placeholder="Password"/><br>

<input type="submit" name="login" value="Login"/>
<input type="submit" name="register" value="Register"/>
</form>
<br>
Clicked submit name: <span id="name_submit">(none)</span>

我已经对此进行了测试,它可以在 IE7 和 Firefox(至少是当前版本)中运行。

关于javascript - 替代用于 chrome 和 IE 8 的 explicitOriginalTarget.id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8067825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com