0

我遇到了有关此代码的两个问题:

  1. 当我调用“window.location.href = 'www.google.com';”时,我无法链接到另一个网站 在 javascript 闭包中。

  2. 此代码在 IE 浏览器中不起作用(提交按钮消失)

下面是代码:

HTML 部分:(由“创建文件夹”按钮控制的隐藏表单)

<a href="#" class="mylink" title="Create Folder" onClick="modalPopup(0);">Create folder</a>
    <!-- modal content -->
    <div id="modal-content">
        <div id="modal-title"></div>
        <div id="modal-text"></div>
        <form name="input" action="" method="post">
            <div id="modal-field"></div>
            <input type="hidden" name="ID" id="ID">
            <input type="hidden" name="submitButton" id="submitButton">
        </form>
    </div>

Javascript 部分:(显示表单并处理提交事件)

function deleteNode( elementId ) 
{
    var label=document.getElementById( elementId ); 
    while ( label.hasChildNodes() ) 
    { 
        label.removeChild( label.lastChild ); 
    }
}

function sendRequest( type, param )
{
    if ( type == "createFolder" )
    { 
        alert('create folder : ' + param ); 
        **window.location.href = "www.google.com";  //  just for test, but not working !**
    }
    return false;
}

function modalPopup( fileID ) 
{
    deleteNode("modal-title");
    deleteNode("modal-text");
    deleteNode("modal-field");

    var titleObj = document.createElement("h3");
    titleObj.innerText = "Create Folder";
    titleObj.textContent = "Create Folder";
    document.getElementById("modal-title").appendChild(titleObj);

    var textObj = document.createElement("a");
    textObj.innerText = "Enter new folder name";
    textObj.textContent = "Enter new folder name";
    document.getElementById("modal-text").appendChild(textObj);

    var fieldObj = document.createElement("input");
    fieldObj.setAttribute("type","input");
    fieldObj.setAttribute("name","createFolder");
    fieldObj.setAttribute("value","New Folder");
    fieldObj.setAttribute("id","modal-field");
    document.getElementById("modal-field").appendChild(fieldObj);

    // assign text field value
    document.getElementsByName('submitButton')[0].type = "submit";
    document.getElementsByName('submitButton')[0].value = "Create";
    **document.getElementsByName('submitButton')[0].onclick = function() { return sendRequest( "createFolder", fieldObj.value ); };**
    document.getElementsByName('ID')[0].value = fileID;             
}
4

1 回答 1

1

jbaby 是对的:

<input type="hidden" name="submitButton" id="submitButton">

应该:

<input type="submit" name="submitButton" id="submitButton">

放一个

return false;

在您绑定到提交按钮的功能结束时

并尝试:

window.location.href = "http://www.google.com";
于 2012-09-17T17:29:04.193 回答