Ajax.BeginForm()的onSuccess - 获取事件目标 [英] Ajax.BeginForm(), OnSuccess - get event target

查看:255
本文介绍了Ajax.BeginForm()的onSuccess - 获取事件目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能得到它发射的元素的目标的onSuccess() Ajax.BeginForm方法()。因此,这里是code片断:

  @using(Ajax.BeginForm(InsertModel,家,新AjaxOptions
{
    列举HTTPMethod =POST
    的onSuccess =的doWork(这一点,SomeCustomText')
}))
{
    < D​​IV ID =容器>
       <! - 一些HTML - >
      <输入类型=提交VALUE =OK/>
    < / DIV>
}<脚本>
功能的doWork(E,customText)
{
   警报(customText); //它显示SomeCustomText',所以它的好。   警报(E); // [对象]对象
   警报(e.prop(变量名)); //对象#<对象>没有方法'道具'
   警报(e.attr(变量名)); //对象#<对象>没有方法ATTR
   警报(jQuery的(E)的.html()); //未定义
   警报(jQuery的(E).prop(变量名)); //未定义
   警报(e.target); //未定义
   警报(jQuery的(E).TARGET); //未定义
 }
<脚本/>

问题:


  

如何获得目标?谢谢


更新1

jQuery的版本应该是这样的:

  jQuery.ajax({
    网址:/首页/ InsertModel
    数据:后的一些数据
    键入:POST,
    成功:功能(数据){
          的doWork(此,数据); //所以我真的不关心数据!我只需要jQuery的(本)
    }
  })


解决方案

如果您要访问你有很多选择的表单元素,如果只有你可以做网页上的一个形式 $( 形式)和jQuery会给你的表单元素。

另一种选择是改变Ajax.BeginForm构造函数的形式id作为像这样的参数:

  @using(Ajax.BeginForm(InsertModel,家,空,新AjaxOptions
{
    列举HTTPMethod =POST
    的onSuccess =的doWork('SomeCustomText')
},{新的id =myFormId}))
{
    < D​​IV ID =容器>
       <! - 一些HTML - >
      <输入类型=提交VALUE =OK/>
    < / DIV>
}

和在JavaScript

 <脚本>
功能的doWork(customText)
{
   警报(customText); //它显示SomeCustomText',所以它的好。   //找到ID形式
   $(#myForm1);   //发现在页形式
   $(形式)
 }
<脚本/>

在平原JQuery的:

  $。阿贾克斯({
    网址:/首页/ InsertModel
    数据:后的一些数据
    键入:POST,
    成功:功能(数据){
       //找到ID形式
       $(#myForm1);       //发现在页形式
       $(形式)       ...
    }
  });

I can't get the the target of the element which fired OnSuccess() method within Ajax.BeginForm(). So here is the code snippet:

@using (Ajax.BeginForm("InsertModel", "Home", new AjaxOptions
{
    HttpMethod = "POST",
    OnSuccess = "doWork(this,'SomeCustomText')"
}))
{
    <div id="container">
       <--Some HTML--!>
      <input type="submit" value="OK"/>
    </div>
}

<script>
function doWork(e,customText)
{
   alert(customText);  //It shows 'SomeCustomText', so its good.

   alert(e);  //[Object] object
   alert(e.prop("tagName"));  //Object #<Object> has no method 'prop' 
   alert(e.attr("tagName"));  //Object #<Object> has no method 'attr' 
   alert(jQuery(e).html());  //undefined
   alert(jQuery(e).prop("tagName"));  //undefined
   alert(e.target);  //undefined
   alert(jQuery(e).target);  //undefined
 }
<script/>

The Question:

How to get target?! Thank you

Update 1

The jQuery version should look like this:

jQuery.ajax({
    url:"/Home/InsertModel",
    data:"some post data",
    type:"POST",
    success:function(data){ 
          doWork(this,data); // so i really do not care about data! I just need jQuery(this)
    }
  })

解决方案

If you want to access the form element you have many options, if you only have one form on the page you can do $("form") and jquery will give you the form element.

Another option is changing the Ajax.BeginForm constructor that takes the form id as parameter like so:

@using (Ajax.BeginForm("InsertModel", "Home",null, new AjaxOptions
{
    HttpMethod = "POST",
    OnSuccess = "doWork('SomeCustomText')"
}, new {id = "myFormId"}))
{
    <div id="container">
       <--Some HTML--!>
      <input type="submit" value="OK"/>
    </div>
}

And in javascript

<script>
function doWork(customText)
{
   alert(customText);  //It shows 'SomeCustomText', so its good.

   // find the form by id
   $("#myForm1");

   // find forms in the page
   $("form")


 }
<script/>

In plain JQuery:

$.ajax({
    url:"/Home/InsertModel",
    data:"some post data",
    type:"POST",
    success:function(data){ 
       // find the form by id
       $("#myForm1");

       // find forms in the page
       $("form")        

       ...
    }
  });

这篇关于Ajax.BeginForm()的onSuccess - 获取事件目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆