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条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制