douxie4583 2013-09-25 06:25
浏览 214
已采纳

在模态对话框中添加三个提交按钮?

And my second question ,,

I have a simple modal box opening when I click on 'add users' . The modal box contains 3 submit buttons ( add, delete, and submit) . I have done this with the help of a javascript. The html code is

<div id="overlay">
<div><a href='#' onclick='overlay()'>X</a>
<form action="" method="POST">  
<table>
  <tr>
    <td style="width:120px;">
        user<input style="width:100px;" name="u" type="text"/> 
    </td>

    <td>        
        <input type ="submit" value = "add"> <input type="submit" value="delete"><br>
    </td>
  </tr>

  <tr>
    <td>
        <select style="width:150px;">
            <option value="abc">abc</option>
        </select>
        <br>
    </td>
  </tr>
</table>
</form>
<input type="submit" value="Submit"/> </div></div><a href='#' onclick='overlay()'>Click here to add user</a>

and my javascript is

function overlay() {
    el = document.getElementById("overlay");
    el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";}

now my task is to to add a user when user types a name in the text field and press a add button, the same user name should come in the drop down list and similarly if user selects one name from the dropdown list and press the delete button , it should delete that name .

And finally if he press the submit button then only the modal box should close and the datas will be shown on my main page..

I have just started doing my task and am planning to implement these functionality using php and some file operations .

My first question is as user first press the add button ,, it will close the modal window and that is not what i am required ( I guess you understand my requirement ) Is there any way to solve this? Please help this newbie Thank you.

  • 写回答

2条回答 默认 最新

  • dqingn8836 2013-09-25 06:46
    关注

    You have to add eventsListeners to the button and prevent their default behave.

    (function(){
        var addButton = document.getElementById("add_btn");
        var deleteButton = document.getElementById("delete_btn");
        addButton.addEventListener("click", addUser, false);    
        deleteButton.addEventListener("click", deleteUser, false);  
    })();
    
    function addUser(event) {
        event.preventDefault();
        var element = event.target;
        var userNameInput = document.getElementById("user_text");
        var userName = userNameInput.value;
    
        var usersList = document.getElementById("users_list");   
        var user = document.createElement('option');
        user.value = userName;
        user.innerHTML = userName;
        usersList.appendChild(user);
        usersList.selectedIndex = usersList.length - 1;
    
        userNameInput.value = "";
    }
    
    function deleteUser(event) {
        event.preventDefault();
        var usersList = document.getElementById("users_list"); 
        usersList[usersList.selectedIndex].remove();
    }
    

    Here an example I wrote on jsFiddle of your code:
    jsFiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度