代码之家  ›  专栏  ›  技术社区  ›  Keith Power

javascript停止按钮onClick from submitting form

  •  0
  • Keith Power  · 技术社区  · 4 年前

    我试过很多方法来阻止这种屈服,但似乎没有一种奏效。当我通过id获取按钮并使用事件监听器时,这是可以的,但是对于在年龄加载之后添加的按钮,这不起作用。我正在试图找到一个解决办法,将与按钮。一个是在页面加载时加载的,另一个是通过JavaScript动态添加的。

                    <table id="conditions-table">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Level</th>
                                <th></th>
                            </tr>
                            <tr>
                                <td>
                                    <input id="condtitions-input"></input>
                                    <select id="condtitions-level">
                                        <option value="Mandatory">Mandatory</option>
                                        <option value="Important">Important</option>
                                        <option value="Support">Support</option>
                                    </select>
                                </td>
                                <td>
                                    <button id="add-condtition" onclick="addCondition(e); return false;">Add Conditions</button></td>
                                </td>  
                            </tr>
                        </thead>
                    </table>
                </fieldset>
                <?= $this->Form->button(__('Submit')) ?>
                <?= $this->Form->end() ?>
            </div>
        </div>
    </div>
    <script>
        var counter = 0;
    
        function addCondition(e){
            e.preventDefault()
    
            var table = document.getElementById("conditions-table");
            var row = table.insertRow(2);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
    
            var condtionsInput = document.getElementById("condtitions-input");
            var condtionsInputValue = condtionsInput.value;
            condtionsInput.value = "";
    
            var selectedLevel = document.getElementById("condtitions-level");
            var selectedLevelValue = selectedLevel.value;
            
            cell1.innerHTML = `<input type="text" name="strategies_conditions[${counter}][name]" value=" ${condtionsInputValue}"></input>
                                <select>
                                    <option ${(selectedLevelValue == "Mandatory") ? 'selected="selected"' : ""} value="Mandatory">Mandatory</option>
                                    <option ${(selectedLevelValue == "Important") ? 'selected="selected"' : ""} value="Important">Important</option>
                                    <option ${(selectedLevelValue == "Support") ? 'selected="selected"' : ""} value="Support">Support</option>
                                </select>`;
            cell2.innerHTML = "<button class='remove-condition' onclick="removeCondition()">X</button></td>";
    
            counter++;
    
            return false;
        };
    
        function removeCondition() {
           // event.target will be the input element.
           var td = event.target.parentNode; 
          var tr = td.parentNode; // the row to be removed
          tr.parentNode.removeChild(tr);       
        };
    
    3 回复  |  直到 4 年前
        1
  •  2
  •   Unmitigated    4 年前

    按钮的默认类型是 "submit" "button" .

    cell2.innerHTML = "<button type='button' class='remove-condition' onclick='removeCondition()'>X</button></td>";
    

    你还需要定义 event 作为事件处理程序函数的参数。

    function removeCondition(event) {
          // event.target will be the input element.
          var td = event.target.parentNode; 
          var tr = td.parentNode; // the row to be removed
          tr.parentNode.removeChild(tr);       
    };
    
        2
  •  0
  •   Eslam Adel    4 年前

    event.target

    document.addEventListener( "click", listenerFunction );
    
    function listenerFunction(event){
        var element = event.target;
        // here you check for that auto generated element
        if(element.tagName == 'A' && element.classList.contains("someBtn")){
            console.log("hi");
        }
    }
    
        3
  •  0
  •   Umutambyi Gad    4 年前

    只是不要插入论点 e 内部 onclick

    btn.onclick = e => {
      e.preventDefault();
    }
    <form>
      <input type="text" name="" placeholder="Name">
      <input type="submit" name="" id="btn">
    </form>

    或者你可以简单地 单击

    <form>
      <input type="text" name="" placeholder="Name">
      <input type="submit" name="" id="btn" onclick="return false">
    </form>