Javascript 如何获取id元素

Javascript 如何获取id元素,javascript,asp.net-mvc-4,Javascript,Asp.net Mvc 4,我做了ActionLink,它打开了PartialView,但我需要在这个PartialView中输入单击元素的Id。我该怎么做 <script type="text/javascript"> //Below code will clear cache $.ajaxSetup({ cache: false }); //Below code will run the JQuery when document is ready. $(docume

我做了ActionLink,它打开了PartialView,但我需要在这个PartialView中输入单击元素的Id。我该怎么做

<script type="text/javascript">
    //Below code will clear cache
    $.ajaxSetup({ cache: false });
    //Below code will run the JQuery when document is ready.

        $(document).ready(function ()
        {//if you see that code in the partial view our submit button
            //is defined as .close class, so below code says on document ready
            //append click event to this button           
            $(".close").live("click", function (e) {
                //prevent default action on the button click
                e.preventDefault();
                //get value of name textbox id=Namecontact
                //put in variable name
                // HERE I WANT to get ID (see code below)
                var t = $("#idtable").val();
                var name = $("#Namecontact").val();
                //get value of email textbox id=emailcontact
                //put in variable email
                var email = $("#emailcontact").val();
                //get value of message textarea id=messagecontact
                //put it in variable message
                //note since messagecontact is a textarea
                //we need to use .text() rather than .val()
                var message = $("#messagecontact").text();
                //Start ajax call
                $.ajax({
                    //define method for sending data to controller
                    type: "POST",
                    //define controller URL
                    url: "/Home/ContactUs",
                    //define data to send to controller
                    //if you remember our controller accepts
                    //3 parameters name, email and message
                    data: { "name": name, "email": email, "message": message },
                    //create a success function
                    //this method is executed if controller
                    //defined by the URL is found
                    //and it accepts the data sent
                    //control comes to this point if the controller
                    //is executed. So data in function(data)
                    //contains the Json data we sent
                    //from the controller
                    success: function (data) {
                        //Now in the div with id result defined in
                        //index page previously we will put in the
                        //data. As you can see Json data is very 
                        //easy way of returning to ajax call because
                        //now you can simply say data(dot)variable sent
                        //and you will get the returned value.
                        $("#result").html("<ul><li>Name: " + data.nameret + "</li><li>Email: " + data.emailret + "</li><li>Message: " + data.messageret + "</li><li>Table: "+data.TableNumber+"</li></ul>");
                        //now close the modal dialog we opened
                        $(".dialog").dialog("close");
                    },
                    //control comes to this function if ajax doesn't
                    //find the controller or controller doesn't accepts
                    //the data sent or controller throws exception

                    error: function (data) {
                        //If it happens alert user of the error
                        alert("There was error processing this");
                        //close the modal dialog
                        $(this).closest(".dialog").dialog("close");
                    }
                });

            });
            //Below code will append a click event to the class openDialog
            //which we assigned to the anchor previously
            $(".openDialog").live("click", function (e) {

                var TableId = window.event.srcElement.id;
                //Below code will prevent default action from occuring
                //when openDialog anchor is clicked
                e.preventDefault();

                // Here I want to get Id of elemnt, and save it in elevent value 
                //idtable        
                $('#idtable').val('TableId');
                //This is the actual implementation of the dailog
                $("<div></div>")
                //To the div created for dialog we will add class named dialog
                //this is done so that we can refer to the dialog later
                //we will see this in a short while why it is important
                .addClass("dialog")
                //add attribute add id attribute
                //note id attribute is assigned the same value as data-dialog-id
                //attribute in openDialog anchor
                .attr("id", $(this).attr("data-dialog-id"))

                //below code appends this div to body
                .appendTo("body")
                //below code describes the attribute for the dialog
                .dialog({
                    //below code assigns title to the dialog
                    //here we use same title as data-dialog-title attribute
                    //in openDialog anchor tag
                    title: $(this).attr("data-dialog-title"),

                    //this will append code to close the dialog
                    //and also put close cross(x) in dialog
                    //we press ESC key in keyboard will also close dialog
                    close: function () { $(this).remove() },
                    //below code defines that the dialog is modal dialog
                    modal: true
                })
                //below code says take href from openDialog anchor
                //which is /Home/ContactUs and load it to the modal dialog
                .load(this.href);
            });
        }           
    );

//下面的代码将清除缓存
$.ajaxSetup({cache:false});
//当文档准备就绪时,下面的代码将运行JQuery。
$(文档).ready(函数()
{//如果您在部分视图中看到该代码,请单击“提交”按钮
//定义为.close类,所以下面的代码在文档上显示为ready
//将单击事件附加到此按钮
$(“.close”).live(“单击”),函数(e){
//防止对按钮单击执行默认操作
e、 预防默认值();
//获取name textbox id=Namecontact的值
//输入变量名
//在这里我想获得ID(见下面的代码)
var t=$(“#idtable”).val();
变量名称=$(“#名称联系人”).val();
//获取电子邮件文本框id=emailcontact的值
//放入可变电子邮件
var email=$(“#emailcontact”).val();
//获取消息textarea id=messagecontact的值
//把它放在变量消息中
//注意,因为messagecontact是一个文本区域
//我们需要使用.text()而不是.val()
var message=$(“#messagecontact”).text();
//启动ajax调用
$.ajax({
//定义向控制器发送数据的方法
类型:“POST”,
//定义控制器URL
url:“/Home/ContactUs”,
//定义要发送到控制器的数据
//如果你还记得我们的控制器接受
//3参数名称、电子邮件和消息
数据:{“name”:name,“email”:email,“message”:message},
//创建一个成功函数
//如果控制器
//找到由URL定义的
//它接受发送的数据
//如果控制器
//执行。因此函数中的数据(数据)
//包含我们发送的Json数据
//从控制器
成功:功能(数据){
//现在在中定义了id result的div中
//索引页之前,我们将在
//正如您所看到的,Json数据非常有用
//返回ajax调用的简单方法,因为
//现在您可以简单地说数据(点)变量已发送
//您将得到返回的值。
$(“#result”).html(“
  • 名称:“+data.nameret+”
  • 电子邮件:“+data.emailret+”
  • 消息:“+data.messageret+”
  • 表格:“+data.TableNumber+”
    • ”; //现在关闭我们打开的模态对话框 $(“.dialog”).dialog(“关闭”); }, //如果ajax不支持,则控件将使用此函数 //查找控制器或控制器不接受 //发送的数据或控制器引发异常 错误:函数(数据){ //如果发生这种情况,请提醒用户该错误 警报(“处理此错误”); //关闭模式对话框 $(此).close(.dialog”).dialog(“关闭”); } }); }); //下面的代码将向openDialog类附加一个click事件 //我们之前分配给主播的 $(“.openDialog”).live(“单击”,函数(e){ var TableId=window.event.srcement.id; //下面的代码将防止发生默认操作 //单击openDialog锚点时 e、 预防默认值(); //这里我想获取elemnt的Id,并将其保存在elevent值中 //可识别 $('#idtable').val('TableId'); //这是dailog的实际实现 $("") //在为dialog创建的div中,我们将添加名为dialog的类 //这样做是为了我们以后可以参考该对话框 //我们将在短时间内看到这一点为什么它很重要 .addClass(“对话框”) //添加属性添加id属性 //注释id属性的值与数据对话框id的值相同 //openDialog锚中的属性 .attr(“id”,$(此).attr(“数据对话框id”)) //下面的代码将此div附加到正文 .附件(“正文”) //下面的代码描述了对话框的属性 .对话({ //下面的代码为对话框指定标题 //这里我们使用与数据对话框标题属性相同的标题 //在openDialog中定位标记 标题:$(this.attr(“数据对话框标题”), //这将附加代码以关闭对话框 //并在对话框中放置闭合十字(x) //我们在键盘上按ESC键也会关闭对话框 关闭:函数(){$(this).remove()}, //下面的代码定义该对话框是模态对话框 莫代尔:对 }) //下面的代码表示从openDialog锚中获取href //即/Home/ContactUs,并将其加载到模式对话框中 .load(this.href); }); } );

或者,如果你能提出另一种解决方案,那就太好了。我需要打开模式窗口,用户可以在这里输入一些信息
@Html.ActionLink(
      "test",          // linkText
      "myAction",      // actionName
      "MyController",  // controllerName
      new {                  // routeValues
           myID = Model.someID 
          },
     null  // htmlAttributes)
 public PartialViewResult myAction(int myID)
    {             
         myData=//do whatever with your id   
        return PartialView(myData);
    }
     @foreach (var item in Model)
       {

     @Html.ActionLink(" ", "ContactUs", "Home", new { myId = item.IdTable}, new { @class =               "openDialog", 
     data_dialog_id = "emailDialog", value = "1", data_dialog_title = "contactus",     @id = "_Logo" + item.IdTable })
      }
       public ActionResult ContactUs(int myId, string name,string email,string message)
     {    /*Your other processing logic will go here*/
         return Json(new
        {
         nameret=name,
           emailret=email,
            messageret=message
         },JsonRequestBehavior.AllowGet);
        }