javascript 停止按钮 onClick 提交表单

javascript stop button onClick from submitting form

我在表单中有两个按钮,我不想提交表单,而是添加和删除 table 行。动态添加一个按钮

我尝试了很多方法来阻止提交,但 none 似乎有效。当我通过 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);       
    };

只是不要在标记的 onclick 事件中插入参数 e 您可以使用 JavaScript 应用事件,如下所示

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

或者您可以简单地使 onclick 事件 return 为 false,如下所示

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

要向 DOM 上尚不存在的元素添加事件,您需要了解 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");
    }
}

按钮的默认类型是"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);       
};