单独的js文件中的Javascript [英] Javascript in seperate js file
本文介绍了单独的js文件中的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在单独的文件中提供并在同一文件中正常工作,我的JS代码无效。这是我的代码。
ASPX:
Hi,
My JS code is not working if i give that in seperate file and working fine in the same file. Here is my code.
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TwoInstances.aspx.cs" Inherits="TwoInstances" %>
<%@ Register src="TwoInstances.ascx" tagname="TwoInstances" tagprefix="uc1" %>
<!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>
<div style="float:left;width:20%;"><uc1:TwoInstances ID="TwoInstances1" runat="server" /></div>
<div style="float:left;width:20%;"><uc1:TwoInstances ID="TwoInstances2" runat="server" /></div>
</div>
</form>
<script type="text/javascript">
function fnCheck() {
var a = document.getElementById('<%=((TextBox)TwoInstances1.FindControl("txt_text")).ClientID%>').value;
if (a == null || a == "")
alert("Please enter some text");
return false;
}
function fnCheck1() {
var b = document.getElementById('<%=((TextBox)TwoInstances2.FindControl("txt_text")).ClientID%>').value;
if (b == null || b == "")
alert("Please enter some text");
return false;
}
</script>
<%-- <script src="Scripts/TextJS.js" type="text/javascript"></script>--%>
</body>
</html>
ASPX.CS:
ASPX.CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TwoInstances : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button btnSubmit1 = (Button)TwoInstances1.FindControl("btn_submit");
btnSubmit1.Attributes.Add("onClick", "fnCheck()");
btnSubmit1.Click += new EventHandler(btnSubmit1_Click);
Button btnSubmit2 = (Button)TwoInstances2.FindControl("btn_submit");
btnSubmit2.Attributes.Add("onClick", "fnCheck1()");
btnSubmit2.Click += new EventHandler(btnSubmit2_Click);
}
void btnSubmit1_Click(object sender, EventArgs e)
{
((TextBox)TwoInstances2.FindControl("txt_text")).Text += ((TextBox)TwoInstances1.FindControl("txt_text")).Text;
}
void btnSubmit2_Click(object sender, EventArgs e)
{
((TextBox)TwoInstances1.FindControl("txt_text")).Text += ((TextBox)TwoInstances2.FindControl("txt_text")).Text;
}
}
如果我在单独的文件中提供相同的javascript代码,则无效。任何人都可以PLZ
给我解决方案。
If i give the same javascript code in seperate file it is not working. Can anyone plz
give me the solution for this.
推荐答案
你不能将服务器端代码用于单独的JS文件中/>
You can't use server side code into a separate JS file
'<%=((TextBox)TwoInstances1.FindControl("txt_text")).ClientID%>'
解决方案:
创建将参数函数放入JS文件
Solution:
create function with parameter into a JS file
function fnCheck(a) {....}
并从aspx页面调用该函数,例如
and call the function from aspx page like
<script>
fnCheck('<%=((TextBox)TwoInstances1.FindControl("txt_text")).ClientID%>');
</script>
这篇关于单独的js文件中的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文