简体   繁体   English

Javascript无法在Chrome中使用? 但在FF和IE作品中

[英]Javascript not working in Chrome?? but in FF and IE works

I am trying to create a button using a div. 我正在尝试使用div创建一个按钮。 This div button will call a linkbutton onlick event. 此div按钮将调用linkbutton onlick事件。 for some reason its not working in chrome. 由于某些原因,它无法在Chrome中使用。 Its not firing the button event in Chrome? 它不会在Chrome中触发按钮事件吗? But it works fine in other browsers.. 但是在其他浏览器中也可以正常工作。

Here is my code.. 这是我的代码。

<%@ 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>

Code behind: 后面的代码:

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>

You can't call an anchor tag's click event from javascript directly, at least in some browsers. 至少在某些浏览器中,您无法直接从javascript调用锚标记的click事件。

There is an example of how to do it here: How can I simulate a click to an anchor tag? 这里有一个如何执行此操作的示例: 如何模拟对锚标记的单击?

Also, you could use jQuery, as I'm pretty sure that handles all the nasty stuff for you. 另外,您可以使用jQuery,因为我敢肯定它可以为您处理所有令人讨厌的事情。

You might want to check : How can I simulate a click to an anchor tag? 您可能要检查一下: 如何模拟对锚标记的点击?

The problem is that DOMElement.click() method is not supported by every browsers, and firefox just implemented it recently for exemple. 问题是并非所有浏览器都支持DOMElement.click()方法,而firefox只是最近才实现了它。

The link I provided provides an alternative, or you could use jQuery ;) 我提供的链接提供了替代方法,或者您可以使用jQuery;)

IF you want to use .click() you have to use jQuery and include it, what you did not do ! 如果要使用.click(),则必须使用jQuery并将其包含在内,这是您没有做的!
jQuery Startup Tutorial: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery jQuery Startup Tutorial: http : //docs.jquery.com/Tutorials : Getting_Started_with_jQuery

EDIT: Sorry, posted before I saw the source. 编辑:对不起,在我看到源之前发布。

First and foremost, isn't value and selected reserved words? 首先, valueselected保留字不是吗? That may present problems in older versions of IE. 在较旧版本的IE中可能会出现问题。

You are triggering a click event on an anchor that has no onclick handler. 您正在没有onclick处理程序的锚上触发click事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM