html onclick运行javascript函数无法正常工作

时间:2012-01-27 09:33:45

标签: javascript html jsp

我正在尝试在html中的input =“image”上运行javascript menthod。我在html中的代码如下所示:

onclick="manageHandlers('Unassign'); document.getElementsByName('manageHandlers')[0].submit(); "

在我的javascript中,方法是这样的:

function manageHandlers(parameter){
   alert("It's working: "+parameter);
}

我有什么问题吗?我可以在昨天知道这个工作,但今天它没有做任何事情。

感谢您的关注!

完整的HTML代码:

    <%-- 
    Document   : pathologist
    Created on : 16 Nov 2011, 09:53:58 AM
    Author     : dean.grobler
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@ page import="javax.portlet.*"%>
<%@ page import="co.za.lancet.medelogget.*" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.liferay.portal.kernel.servlet.*" %>
<%@page import="com.liferay.portal.kernel.util.Validator" %>

<portlet:defineObjects />

<%
    String editable = request.getParameter("editable");
    String selectedValue = request.getParameter("selectedValue");
    int selectedIndex = 0;
    if (selectedValue != null) {
        selectedIndex = Integer.parseInt(selectedValue);
    }
    ArrayList<String> data = tableData.getTableData(selectedIndex, "consumers", "code");
    String AvailableJavaScript = tableData.consumersAvailableHandlers(data.get(0));  
    String AssignedJavaScript = tableData.consumersAssignedHandlers(data.get(0)); //data.get(0) is the whereclause    

%>

<!-- -------------------------Interface Starts here------------------------- -->

<aui:layout>
    <form name="consumerDetails" action="<portlet:actionURL ><portlet:param name="consumers" value="consumers"/></portlet:actionURL>" method="post">
        <input type="hidden" value="consumers" name="formType" />
        <input type="hidden" value="<%=selectedValue%>" name="selectedValue" />



    <input type="hidden" value="" id="button" name='button' />
    <aui:column columnWidth="50">
        <h3>
            <%if (editable.equals("")) {%>
            Edit Consumer
            <%} else {%>
            Consumer Details 
            <%}%>
            <%%>
        </h3>   
        <div class="horizontalRules"><hr class></div>
        <div id="successAndErrorMessages"></div>

        <table width="100%">
            <tr height="35px"><td width="25%">Code:</td><td><input type="text" value="<%=data.get(0)%>" name="code"  class="textBoxes" <%=editable%>></td></tr>
            <tr height="35px"><td width="25%">Description:</td><td><input type="text" value="<%=data.get(1)%>" name="description"  class="textBoxes" <%=editable%>></td></tr>           
        </table>
    </aui:column>    

    </form>
    </aui:layout>

    <!-- --------------------Handler rules section---------------------- -->        
    <%if (editable.equals("")) {%>

    <aui:layout>
        <aui:column columnWidth="100">
            <h3>Manage Handler Rules</h3>
            <div class="horizontalRules"><hr class></div>                        
            </aui:column>
        </aui:layout>

    <aui:layout>

        <form name="manageHandlers" id="formType" action="<portlet:actionURL ><portlet:param name="manageHandlers" value="manageHandlers"/></portlet:actionURL>" method="post">

            <!-- index selected in handlers tables -->
            <input type="hidden" name="selectedHandler" id="selectedHandler" /> 
            <input type="hidden" value="" id="handlerButton" name='handlerButton' />
            <input type="hidden" value="" id="button" name='button' />
            <input type="hidden" value="" id="tableType" name='tableType' />
            <input type="hidden" value="manageHandlers" name="formType" />

            <table width="100%">
                <tr><td width="35%"><table width="100%">
                            <tr>
                                <td><h4 style="margin-bottom: 10px;">Available Rules</h4></td>
                            </tr>
                            <tr>
                                <td><div class="availableTable" id='available_div'></div></td>
                            </tr>
                        </table></td>

                    <td width="15%"><div class="manageHandlersButtons1">
                            <table width="100%">
                                <tr>
                                    <td><h4>Fatal Error:</h4></td>
                                </tr>
                                <tr>
                                    <td><input type="checkbox" id="fatal" name="fatal" class="manageHandlersCheckbox" value="1" /></td>                                        
                                </tr>
                                <tr>
                                    <td><input type="image" id="button" value="Assign" src="<%=renderRequest.getContextPath()%>/Images/rightarrow.png" alt="Assign Selected Rule" class="imgAssignUnassign" onclick="if(manageHandlers('Assign')){ document.getElementsByName('manageHandlers')[0].submit(); }" /></td>
                                </tr>
                                <tr>
                                    <td><input type="image" id="button" value="Unassign" src="<%=renderRequest.getContextPath()%>/Images/leftarrow.png" alt="Unassign Selected Rule" class="imgAssignUnassign" onclick="manageHandlers('Unassign'); document.getElementsByName('manageHandlers')[0].submit(); " /></td>
                                </tr>
                            </table>
                        </div></td>

                    <td width="35%"><table width="100%">
                            <tr>
                                <td><h4 style="margin-bottom: 10px;">Assigned Rules</h4></td>
                            </tr>
                            <tr>
                                <td><div class="assignedTable" id='assigned_div'></div></td>
                            </tr>
                        </table></td>

                    <td width="15%"><div class="manageHandlersButtons2">
                            <table width="100%">
                                <tr>
                                    <td><input type="button" id="button" name="MoveUp" value="MoveUp" style="width: 85px;" class="manageHandlersMvUp" onclick="if(manageHandlers('Move up')){ document.getElementsByName('manageHandlers')[0].submit(); }" /></td> 
                                </tr>
                                <tr>
                                    <td><input type="button" id="button" name="MoveDown" value="MoveDown" style="width: 85px;" class="manageHandlersMvDwn" onclick="if(manageHandlers('Move down')){ document.getElementsByName('manageHandlers')[0].submit(); }" /></td>
                                </tr>
                            </table></div>

                    </td></tr>
            </table>
        </form>                                

    </aui:layout>        

    <div class="horizontalRules2"><hr class></div>          

    <%}%>

    <table width="100%">
        <tr>
            <td width="25%"><input type="button" value="Back" style="width: 80px;" class="leftButtons"  onClick="document.getElementById('button').value='Back'; document.getElementsByName('consumerDetails')[0].submit()" /></td>

            <!-- if not editable, don't display this button -->
            <% if (editable.equals("")) {%>                   
            <td><input type="button" value="Save" style="width: 80px;" class="rightButtons" onClick="if (addEntryValidator('Save','consumers','edit')) {document.getElementsByName('consumerDetails')[0].submit()}" /></td>                    
                <% }%>
        </tr> 
    </table>

    <!-- JavaScript Tables -->    
    <script type="text/javascript"><%= AssignedJavaScript%></script>
    <script type="text/javascript"><%= AvailableJavaScript%></script>

1 个答案:

答案 0 :(得分:3)

这是错的:

onclick="manageHandlers('Unassign'); 
document.getElementsByName('manageHandlers')[0].submit();"

我之前从未见过这样的东西..如果它确实可能并且我确实错过了某些东西,那么我敢打赌很少这样做,因为不将你的js代码与内容层(HTML)分开是不好的做法。

无论如何,onclick是一个事件处理程序。您通常会分配一个在单击时执行的函数。

如果我是你,我不会做内联javascript。但如果你真的想让它保持内联,那么你可以试试这个:

function manageHandlers(parameter){
    alert('blabla' + parameter);
    document.getElementsByName('manageHandlers')[0].submit();
}

和onclick,你可以做onclick="manageHandlers('Unassign');"希望有帮助..