按钮上的onclick用法

时间:2013-01-17 09:14:12

标签: javascript html jsp

我有一个带有3个按钮的 HTML 表单:

<html:form modelAttribute="registration" id="registration-form" formUrl="/registrationForm">
<input type="hidden" name="id"/>
<html:field name="voucherID" labelCode="label.voucherID" type="text"/>
<html:field name="programID" labelCode="label.programID" type="text"/>
<html:field name="quantity" labelCode="label.quantity" type="text"/>
<html:field name="active" labelCode="label.active" type="checkBox"/>

<div class="form-actions">
    <button type="submit" class="btn btn-primary"><spring:message code="button.add"/></button>
    <button type="reset" class="btn"><spring:message code="button.cancel"/></button>
    <button type="button" class="btn" onclick="remove()"><spring:message code="button.delete"/></button>
</div>
</html:form>

Javascript

<head>
<%@ include file="header.jsp"%>
<title><spring:message code="title.registration"/></title>
<script type="text/javascript">
function remove(){
    alert('test');
    var form = document.forms[0];
    form.action = "deleteRegistration";
    form.submit();
}
</script>
</head>

我要做的是,每当用户按下删除按钮时,它应该将操作值更改为deleteRegistration。但是我不能使这个代码工作,甚至根本不会触发警报功能。我哪里做错了?

3 个答案:

答案 0 :(得分:4)

有趣的是,remove()方法将在this对象上运行,将方法重命名为_remove或其他任何内容,它应该按预期工作。

答案 1 :(得分:0)

尝试改变这个:

<button type="button" class="btn" onclick="javascript: remove();"><spring:message code="button.delete"/></button>

答案 2 :(得分:0)

<html>
<head>
<title></title>
<script type="text/javascript">
function remove(){
    alert('test');
    var form = document.forms[0];
    form.action = "deleteRegistration";
    form.submit();
}
</script>
</head>
<body>
        <form id="registration-form" action="">
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">submit</button>
                <button type="button" class="btn">something</button>
                <button type="button" class="btn" onclick="remove()">remove</button>
            </div>
        </form>
</body>
</html>