根据焦点获取两个提交按钮中任何一个的值

Get value of either of the two submit button based on focus

我有一个带有两个提交按钮和两个文本框的 html 表单

这是表格

<form action="domains.php#searchdomain" method="post" name="m_domain"
      onsubmit = "return primalValidate()">
 <table class="dataTable" width="100%" border="0" cellspacing="0" cellpadding="5" id="" style="text-align:center; margin-top:0px; border-left:1px solid #ddd; border-right:1px solid #ddd; border-top:1px solid #ddd;">
          <tr>
            <td align="left" colspan="2"><div id="display_message" <?php echo $sstyle; ?>><?php echo $dis_msg; ?></div></td>
          </tr>
          <tr>
            <td align="left">Search Domain</td>
            <td align="left" style="display:none;" id="apply_text">Replace Selected Domains With</td>
          </tr>

          <tr>
            <td align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td><input class="input_field" name="search_domain" id="search_domain" value="<?php echo $search_domain; ?>" type="text"></td>
                  <td>&nbsp;</td>
                  <td><input type="submit" class="btn blue_button" 
                    name="submit_domain_form" id="submit_search_button"
                  value="Search" /></td>
                </tr>
              </table></td>
            <td align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0" id="apply_button" style="display:none;">
                <tr>
                  <td><input class="input_field" name="domain_name_url" id="domain_name_url" value="<?php echo $domain_name_url; ?>" type="text"></td>
                  <td>&nbsp;
                  <input name="domain_replace_id" id="domain_replace_id" 
                  value="" type="hidden">
                  <input name="domain_replace_link" id="domain_replace_link" 
                  value="" type="hidden">
                  </td>
                  <td><input type="submit" class="btn blue_button" 
                    name="submit_domain_form" id="submit_replace_button"
                  value="Apply" /></td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td align="center">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td><input type="checkbox" name="status_domain" id="status_domain" <?php if($status_domain){?> checked <?php } ?>>&nbsp;Include inactive campaigns in search.</td>
                      <td>&nbsp;</td>
                      <td>&nbsp;</td>
                    </tr>
                </table>
            </td>
            <td align="center">&nbsp;</td>
          </tr>

        </table>     
</form>

现在我正在尝试制作一个像这样的通用验证表单...

function primalValidate()
{
    var action = document.forms["m_domain"]["submit_domain_form"].value;
    if(action=="Search")
    {
        //validation condition 1;
        //return false;
    }
    else if(action=="Apply")
    {
        //validation condition 2;
        //return false;
    }

}

但问题是 none 验证条件有效....

当我试图提醒行动的价值时,即

alert(action); 我收到空白警报

这是表单中的两个提交按钮

<input type="submit" class="btn blue_button" 
                        name="submit_domain_form" id="submit_search_button"
                      value="Search" />

<input type="submit" class="btn blue_button" 
                        name="submit_domain_form" id="submit_replace_button"
                      value="Apply" />

试试这个,

var action = $("input[type=submit]:focus").val();;

Demo

如果您想通过提交按钮名称提交,请尝试,

var action = $("input[name=submit_domain_form]:focus").val();

试一试here

此外,如果您使用回车键提交,那么以上两个都会提示 undefined。为此,您需要根据 HTML 设计的一些逻辑,例如

if(action==undefined){
    action=$('*:focus').closest('table').find(':submit').val();
    // you can use name=submit_domain_form instead of :submit as used in second updated answer
}

working demo