如何使用javascript从表单验证中排除一个按钮

时间:2015-07-24 15:31:45

标签: javascript forms validation

表单会在提交时进行验证,如果可以,您可以添加新值,修改现有值或删除。但是,删除会被验证中断。第一部分验证检查是newName选择中是否已存在readName。第二部分是为了确保文本框不是空白。 newName根据readName选项填充<form action="lists" method="post" onsubmit="return validate(this)"> <selects id="readName" name="readName" onchange="updateAction(this.form, 'read');this.form.submit()"> <option title="PETER" value="PETER">PETER</option> <option selected title="Will" value="Will">Will</option> <option title="one" value="one">one</option> </select> <label class="padLeft15">Edit Name:</label>&nbsp; <input type="text" name="newName" id="newName" title="Will" value="Will" /> <input class="marginRight10" id="new" type="submit" onclick="updateAction(this.form, 'new');" value="New" /> <input class="marginRight10" id="save" type="submit" onclick="updateAction(this.form, 'save')" value="Save" /> <input class="marginRight10" id="delete" type="submit" onclick="updateAction(this.form, 'delete')" value="Delete" /> </form> <script> function validate(form) { var isValid = validateText(form.newName); var exists= $("#readName option[value='"+ $("#newName").val()+"']").length > 0; if (exists) { document.getElementById("error").innerHTML = "WHOAA, already have this in the list!!"; return false; } if (!isValid) { document.getElementById("error").innerHTML = "Name must not be empty"; return false; } return isValid; } </script> 。在删除验证投诉时,因为您提交的是存在于下拉列表中的值。如何删除旁路验证?

express

1 个答案:

答案 0 :(得分:0)

将删除按钮更改为按钮类型,以便不提交。然后通过手动传递数据来覆盖单击操作。

<input class="marginRight10" id="delete" type="button" onclick="updateAction(this.form, 'delete')" value="Delete" />

$('body').on('click', '#delete', function () { 
     updateAction('#yourformid', 'delete')
});