Javascript无法在Chrome中运行?但在FF和IE工作

时间:2012-02-10 13:52:20

标签: c# javascript asp.net

我正在尝试使用div创建一个按钮。此div按钮将调用linkbutton onlick事件。由于某种原因,它不适用于铬。它没有触发Chrome中的按钮事件?但它在其他浏览器中运行良好..

这是我的代码..

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
    ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="dvButton" runat="server" ></div>
       <asp:LinkButton ID="DummyBtn" runat="server" Text="Dummy" OnClick="DummyBtn_Click"></asp:LinkButton>
    <input type="hidden" id="hdnConfigId" runat="server" />
    <input type="hidden" id="hdnSelected" runat="server" />

    </form>
</body>
<script language="javascript" type="text/javascript">
    function Selected(value, selected) {

        document.getElementById('<%=hdnConfigId.ClientID %>').value = value;

        document.getElementById('<%=hdnSelected.ClientID %>').value = selected;

        document.getElementById('<%=DummyBtn.ClientID %>').click(); //This does not work in Chrome

    }
    </script>
</html>

代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

           dvButton.InnerHtml = "<a href=\"javascript:void(0);\" id=1234 onclick=\"javascript:Selected('1234','Test');return false;\" >TEST</a>";
        }

        protected void DummyBtn_Click(object sender, EventArgs e)
        {
           string id = hdnConfigId.Value;
           string selected = hdnSelected.Value;
           //Logic to retrieve data by passing these parameters into DB

        }      
    }
}

RENDERED SOURCE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head><title>



</title></head>

<body>

    <form name="form1" method="post" action="WebForm1.aspx" id="form1">

<div>

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />

<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUHNjcxMjAzMQ9kFgICAQ9kFgICAQ8WAh4JaW5uZXJodG1sBWo8YSBocmVmPSJqYXZhc2NyaXB0OnZvaWQoMCk7IiBpZD0xMjM0IG9uY2xpY2s9ImphdmFzY3JpcHQ6U2VsZWN0ZWQoJzEyMzQnLCdUZXN0Jyk7cmV0dXJuIGZhbHNlOyIgPlRFU1Q8L2E+ZGSqT177YHIKIjfz5j0oOl9lGktifA==" />

</div>



<script type="text/javascript">

//<![CDATA[

var theForm = document.forms['form1'];

if (!theForm) {

    theForm = document.form1;

}

function __doPostBack(eventTarget, eventArgument) {

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

        theForm.__EVENTTARGET.value = eventTarget;

        theForm.__EVENTARGUMENT.value = eventArgument;

        theForm.submit();

    }

}

//]]>

</script>





<div>



    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBALUn5j+AQKEzpmBDwKf7rOjAgLVqJ/GB0fyISUyIuzPcZ4CYvSLltNpE/oA" />

</div>

    <div id="dvButton"><a href="javascript:void(0);" id=1234 onclick="javascript:Selected('1234','Test');return false;" >TEST</a></div>

       <a id="DummyBtn" href="javascript:__doPostBack('DummyBtn','')">Dummy</a>





    <input name="hdnConfigId" type="hidden" id="hdnConfigId" value="1234" />

    <input name="hdnSelected" type="hidden" id="hdnSelected" value="Test" />

    </form>

</body>

<script language="javascript" type="text/javascript">

    function Selected(value, selected) {



        document.getElementById('hdnConfigId').value = value;



        document.getElementById('hdnSelected').value = selected;





        document.getElementById('DummyBtn').click();



    }

    </script>

</html>

4 个答案:

答案 0 :(得分:2)

您无法直接从javascript调用锚标记的点击事件,至少在某些浏览器中是这样。

这里有一个示例:How can I simulate a click to an anchor tag?

另外,你可以使用jQuery,因为我很确定能为你处理所有讨厌的东西。

答案 1 :(得分:1)

您可能想要检查: How can I simulate a click to an anchor tag?

问题是每个浏览器都不支持DOMElement.click()方法,而firefox最近只是为了实例而实现了它。

我提供的链接提供了另一种选择,或者您可以使用jQuery;)

答案 2 :(得分:0)

如果你想使用.click(),你必须使用jQuery并包含它,你没有做什么!
jQuery启动教程: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery

答案 3 :(得分:0)

编辑:对不起,在我看到来源之前发布了。

首先,不是valueselected保留字?这可能会在旧版本的IE中出现问题。

您正在触发没有onclick处理程序的锚点上的click事件。