简体   繁体   English

Javascript函数在IE中不起作用,但在Firefox中起作用

[英]Javascript Function not working in IE but works in Firefox

<script type="text/javascript">
$(document).ready(function () {
    showHome();
}); 

function findTemplate() {
var selectedIndustry = $("#industrySelected option:selected").text();
var selectedTemplate =$("#templateCode").val();
$.ajax({
    type: "post",
    url: "/_layouts/TBSharePointProject/SharePointTestService.asmx/redirectUserToAppropriateTemplate",
    contentType: "application/x-www-form-urlencoded",
    dataType: "xml",
    data: { industry: selectedIndustry, templateCode: selectedTemplate, checkList: "" },
    success: function (result) {
        xmlStr = xmlToString(result);
        xml = removeFirstAndLastLine(xmlStr)
        myJsonObject = xml2json.parser(xml);
        //alert(myJsonObject.eccn[0].eccnno);

        $("#surveyScreen").empty();
        for(var i = 0; i <= myJsonObject.eccn.length; i++) {

            $("#surveyScreen").append("<p><input id='" + myJsonObject.eccn[i].guid + "' type='checkbox' checked ='checked'>" + myJsonObject.eccn[i].eccnno + ": " + myJsonObject.eccn[i].title + "</input></p>");

        }
        $("#surveyScreen").append("<br/><input type='button' id='goHome' value='Back'     onclick =\"javascript: showHome();\"/>");
    },
    error: function (result) {
        alert('error occured');
    },
    async: true
});


}
//Converst xmlString to String
function xmlToString(xmlObj) {
if (navigator.appName == "Netscape") {
    return (new XMLSerializer()).serializeToString(xmlObj);
}
if (navigator.appName == "Microsoft Internet Explorer") {
    return xmlObj.xml;
}
}

function removeFirstAndLastLine(xmlStr) {
// break the textblock into an array of lines
var lines = xmlStr.split('\n');
// remove one line, starting at the first position
lines.splice(0, 2);
// join the array back into a single string
var newtext = lines.join('\n');
//Removes the last line
if (newtext.lastIndexOf("\n") > 0) {
    return newtext.substring(0, newtext.lastIndexOf("\n"));
} else {
    return newtext;
}
}

function showHome() {

$("#surveyScreen").empty();
$("#surveyScreen").append("<p>Do you have a saved checklist?</p>");
$("#surveyScreen").append("<p>Submission Code:<input type='text' id='checkListCode'/>    </p>");
$("#surveyScreen").append("<p><input type='button' id='getCheckList' value='Get Saved     Checklist' onclick =\"javascript: findTemplate();\"/></p><br/><br/>");
$("#surveyScreen").append("<p>Industry</p>");
$("#surveyScreen").append("<select id='industrySelected'>"+
                            "<option>Computer & Networking</option>"+
                            "<option>Biotechnology</option>"+
                            "Industry</select>");
$("#surveyScreen").append("<br/>Or");
$("#surveyScreen").append("<p>Template Code:<input type='text' id='templateCode'/>    </p>");
$("#surveyScreen").append("<p><input type='button' id='getTemplate' value='Next'     onclick =\"javascript: findTemplate();\"/></p><br/><br/>");

}

</script>
<body>
<div id="surveyScreen">
</div>
</body>

Can someone explain to me why this function call works in Firefox but not IE, and what needs to be done for it to work in IE. 有人可以向我解释为什么此函数调用可在Firefox中而不是IE上运行,以及需要做什么才能使其在IE中工作。

So I updated the post to show more of my code..some pieces are left out...and some others are unimportant 因此,我更新了帖子以显示更多代码。.一些片段被省略了...而另一些则不重要

IE is much pickier when it comes to duplicate ids. 当涉及重复的ID时,IE会变得更加挑剔。 ids should (of course) be unique but firefox just grabs the first one and continues. id(当然)应该是唯一的,但是firefox只会获取第一个,然后继续。 IE ignores attempts to access dupe ids. IE会忽略尝试访问重复ID的尝试。

You din't post your HTML but this is the direction i'd look first. 您没有发布HTML,但这是我首先要寻找的方向。

Make sure your not missing a semi-colon anywhere before this line of code is called. 确保在调用此代码行之前,不要在任何地方遗漏分号。 I've found that a missing semi-colon (or any other error) will cause IE to stop working at the spot of the error but it will still work in FF. 我发现缺少分号(或其他任何错误)将导致IE在出现错误时停止工作,但仍然可以在FF中工作。

Your code works fine in IE 9 http://jsfiddle.net/eL2nU/ 您的代码在IE 9中正常工作http://jsfiddle.net/eL2nU/

$("#surveyScreen").append("<p><input type='button' id='getTemplate' value='Next' onclick=\"javascript: findTemplate();\"/></p><br/><br/>");

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

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