输入类型="图像"vs类型="提交"

Phi*_*enn 7 html coldfusion

我编程了很长时间检查StructKeyExists(表单,"更新"),直到我将输入从type ="submit"更改为type ="image".当type ="image"时,IE不会发回控件的名称,而是发送Update.X和Update.Y.

<form method="post">
Old Way:<br />
<input type="submit" value="3" name="Update" /><br />
<input type="submit" value="4" name="Delete" />
<p>New Way:</p>
<input type="image" value="1" name="Update" src="http://www.google.com/intl/en_ALL/images/logo.gif" /><br />
<input type="image" value="2" name="Delete" src="http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif" />
</form>
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是我应该在我的逻辑中添加两个字符

from: <cfif StructKeyExists(form,"Update")
to:   <cfif StructKeyExists(form,"Update.X")
Run Code Online (Sandbox Code Playgroud)

但我想要一个处理type ="submit"和type ="image"的解决方案.现在我的逻辑是:

<cfif StructKeyExists(form,"Update") OR StructKeyExists(form,"Update.X")>
   <!--- UPDATE table --->
<cfelseif StructKeyExists(form,"Delete") OR StructKeyExists(form,"Delete.Y")>
   <!--- DELETE FROM Table --->
</cfif>
Run Code Online (Sandbox Code Playgroud)

问:有没有更优雅的方法来检查按下了哪个按钮?假设表单上有多个按钮,因为如果我只需要检查表单是否已提交,我会检查是否存在form.fieldnames.

Pet*_*ton 11

要获得原始的Form.Update和Form.Delete,同时在按钮上显示图像,请尝试以下操作:

<form action="somewhere" method="post">
    <button type="submit" name="Update"><img src="update.btn.png" alt="Update"/></button>
    <button type="submit" name="Delete"><img src="delete.btn.png" alt="Delete"/></button>
</form>
Run Code Online (Sandbox Code Playgroud)


然后,您需要使用CSS删除默认按钮样式,这样您才能获得图像,例如:

form button
{
    margin       : 0;
    padding      : 0;
    border-width : 0;
    background   : none;
    cursor       : pointer;
}
Run Code Online (Sandbox Code Playgroud)


并确保在内容开始时使用有效的DOCTYPE以防止怪异模式 - 我通常会重置以确保它是第一件事:

<cfcontent reset/><!DOCTYPE html>
Run Code Online (Sandbox Code Playgroud)


小智 1

您还可以检查表单字段列表,看看它是否包含字符串“Update”。就像是:

<cfif StructKeyExists(form,"fieldnames") and form.fieldnames contains "Update">
<!--- Do Update --->
<cfelseif StructKeyExists(form,"fieldnames") and form.fieldnames contains "Delete">
<!--- Do Delete --->
</cfif>
Run Code Online (Sandbox Code Playgroud)

Form.fieldnames 包含已提交的表单字段的所有名称。