Javascript可在Firefox中使用,但在IE中无法使用 [英] Javascript works in firefox but not working in IE
问题描述
您好专家,
我有一些JavaScript函数可用来动态添加/删除文本框.
以下是使用的功能.
Hello experts,
I have a few javascript functions using which i am adding/deleting text box dynamically.
Following are the functions used.
<pre lang="cs"><script type="text/javascript" language="javascript">
function pagevalid() {
if (document.getElementById("ctl00_ContentPlaceHolder1_txtform").value == "") {
alert("Form Name should be entered");
document.getElementById("ctl00_ContentPlaceHolder1_txtform").focus();
return false;
}
}
/*******************To Add Textbox and remove texbox for email address at runtime*******************************/
function GetDynamicTextBox(value) {
var id = 0;
id = id + 1;
//alert(value)
var formid = eval(''<%=formid%>'');
return ''<input name = "DynamicTextBox" id = "'' + ''IDt'' + value + ''" type="text" value = "'' + value + ''" />'' +
''<input type="button" value="Remove" onclick = "RemoveTextBox(this,'' + formid + '','' + String.fromCharCode(39) + value + String.fromCharCode(39) + '' )" />''
// ''<input type="button" id="'' + id + ''" value="Remove" onclick = "RemoveTextBox(this,'' + value + '')" />''
}
function AddTextBox() {
var div = document.createElement(''DIV'');
div.innerHTML = div.innerHTML + GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}
function RemoveTextBox(div, formid, eml) {
alert(div)
//alert(document.getElementById("TextBoxContainer").text.toString())
if (confirm(''Are you sure you want to remove the id??'')) {
alert(eml);
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
PageMethods.Delete(formid, eml);
}
}
function RecreateDynamicTextboxes() {
var values = eval(''<%=Values%>'');
if (values != null) {
var html = "";
for (var i = 0; i < values.length; i++) {
html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes();
/*Validate Email*/
function validateEmail(email) {
//alert(email)
var splitted = email.match("^(.+)@(.+)$");
alert(splitted)
if (splitted == null) return false;
if (splitted[1] != null) {
var regexp_user = /^\"?[\w-_\.]*\"?$/;
if (splitted[1].match(regexp_user) == null) return false;
}
if (splitted[2] != null) {
var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
if (splitted[2].match(regexp_domain) == null) {
var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
if (splitted[2].match(regexp_ip) == null) return false;
} // if
return true;
}
if (document.getElementById("ctl00_ContentPlaceHolder1_txtform").value == "") {
alert("Form Name should be entered");
document.getElementById("ctl00_ContentPlaceHolder1_txtform").focus();
return false;
}
return false;
}
//Called by JS
pageMethodConcept = {
callServerSideMethod: function () {
//aPageMethods.Delete(3, 4, pageMethodConcept.callback);
// I am passing 3 and 4 to get sum and set callback method
},
callback: function (result) {
alert(result);
}
}
//window.onload = pageMethodConcept.callServerSideMethod;
</script>
使用以下代码填充文本框.
Using the following code i am populating the text boxes.
CdataTable = ObjBussiness.admin_select_FormEmail(formid, "1", source).Tables[0];
if (CdataTable.Rows.Count != 0)
{
string str;
int cnt = CdataTable.Rows.Count;
string[] textboxValues = new string[CdataTable.Rows.Count];
for (int i = 0; i < cnt; i++)
{
textboxValues[i] = CdataTable.Rows[i]["email"].ToString();
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
this.Values = serializer.Serialize(textboxValues);
str = "<script language='javascript'>RecreateDynamicTextboxes()</script>";
ClientScript.RegisterStartupScript(this.GetType(), "javascript", str);
}
此方法用于删除通过单击添加按钮动态添加的文本框
This method is used to remove the text box added dynamically by clicking the add button
[System.Web.Services.WebMethod]
public static string Delete(string formid, string email)
{
try
{
string source = ConfigurationManager.ConnectionStrings["pep"].ToString();
bussinessacesslayer bussiness = new bussinessacesslayer();
bussiness.admin_Delete_FormEmail(formid, "1", email, source);
//string str = "sder";
}
catch (Exception e)
{
}
return "Deleted";
}
如果在服务器端填充了该文本框,则可以删除该文本框.如果我单击添加按钮,我可以添加一个文本框,但是当我单击删除按钮时,会出现错误,提示"getElementById(..)为null或对象.."
我做了一些谷歌,但找不到解决方案.
这是我的添加按钮
I am able to delete the text box if its is populated at server side. If i click the add button i am able to add a text box but when i click on remove button it is giving an error saying "getElementById(..) is null or object.."
I did a bit of google but could not find a solution.
This is my add button
<input id="btnAdd" type="button" value="" onclick="AddTextBox()" style="background-image:url(adminimages/Plus.jpeg); width: 19px; background-repeat: repeat; height: 19px; background-color: transparent;" />
假设我在DB中有两个值,然后用这些值初始化我的文本框.现在,如果我单击删除",该功能将正常运行,但是如果我单击添加按钮",则会添加一个文本框,但是,当我单击删除"按钮时,它将无法正常工作.
请帮忙.
我正在使用IE8.
Suppose i have two values in DB, then my text box are initialized with these values. Now if i click on remove, the function works fine but if i click on "add button" a textbox is added but, now when i click on the remove button it does not work.
Please help.
I am using IE 8.
推荐答案
); 警报(已拆分) 如果(splitted == null)返回false; if(splitted [1]!= null){ var regexp_user =/^ \?[\ w -_ \.] * \"?
"); alert(splitted) if (splitted == null) return false; if (splitted[1] != null) { var regexp_user = /^\"?[\w-_\.]*\"?
/; 如果(splitted [1] .match(regexp_user)== null)返回false; } if(splitted [2]!= null){ var regexp_domain =/^ [\ w-\.] * \.[A-Za-z] {2,4}
/; if (splitted[1].match(regexp_user) == null) return false; } if (splitted[2] != null) { var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}
/; if(splitted [2] .match(regexp_domain)== null){ var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]
/; if (splitted[2].match(regexp_domain) == null) { var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]
这篇关于Javascript可在Firefox中使用,但在IE中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!