簡體   English   中英

javascript 停止按鈕 onClick 來自提交表單

[英]javascript stop button onClick from submitting form

我在表單中有兩個按鈕,我不想提交表單但添加和刪除表行。 一個按鈕是動態添加的。

我嘗試了很多方法來阻止提交,但似乎都沒有用。 當我通過 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);       
    };

按鈕的默認類型是"submit" 只需將其設置為"button"即可覆蓋該行為。

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

您還需要將event定義為事件處理程序 function 的參數。

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);       
};

只是不要在標記中的onclick事件中插入參數e您可以使用 JavaScript 應用事件,如下所示

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

或者您可以簡單地使onclick事件返回 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");
    }
}

您真正需要做的就是添加:

 <input type="submit" onclick="event.preventDefault();">

你可能想要處理它,所以總的來說你可能會做更多這樣的事情:

  <script>
function myFunction(){
  if (confirm("Are you sure you want to ...? This action cannot be undone.")) {
  document.getElementById("myForm").submit();
  }

}
  </script>
  <form method="post" action="/test" id="myForm">
  <input type="submit" onclick="event.preventDefault();myFunction();">

  </form>

這允許用戶單擊確定繼續或取消不提交表單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM