Php Jquery:将输入类型从按钮更改为提交

Php Jquery:将输入类型从按钮更改为提交,php,jquery,html,Php,Jquery,Html,我有一个动画搜索表单如下 <form role="search" method="get" id="searchform" action="<?php echo home_url('/'); ?>"> <div id="searchtext"> <input type="text" value="" size="25" name="s" id="s" placeholder="<?php esc_attr_e('Search', 'rev

我有一个动画搜索表单如下

<form role="search" method="get" id="searchform" action="<?php echo home_url('/'); ?>">

<div id="searchtext">
    <input type="text" value="" size="25" name="s" id="s" placeholder="<?php esc_attr_e('Search', 'reverie'); ?>">
</div>

<div id="submitsearch">
    <input type="button" id="searchsubmit" value="" class="button postfix">
</div>
当第一次单击“搜索”按钮时,它会切换出文本区域,用户可以在其中键入他们想要搜索的内容。但是,当他们再次单击“搜索”按钮时,应提交搜索表单(即输入类型应从“按钮”更改为“提交”)


我已经读过一些关于这方面的文章,但是这些都是几年前的&我不确定它们是否仍然相关。无法更改输入的类型吗?

我认为是可能的,因为类型是一个属性。下面的小代码行应该有帮助:

$('#searchsubmit').removeAttr("type").attr("type", "submit");
也许有一种风险更小的方法。方法是添加一个调用submit函数的“onclickEvent”:

 $('#searchsubmit').on('click', function () {
    $(this).parents("form:first").submit();
});

像这样的东西应该可以做到:

$('.button').on('click', function(){
  $("#searchtext").animate({width:'toggle'},500);
  $(this).attr('type', 'submit');
});
它还不完整,但应该会让你更接近你想要的:)

嗯。。。我认为使用最新的jQuery,您甚至可以将('click'function(){})上的
替换为


(旧版本的
.click()
没有
live/on
绑定)

您可以删除type属性并将其再次设置为submit

jQuery("#submitsearch").click(function () {
jQuery("#searchtext").animate({
    width: 'toggle'
}, 500);
jQuery(this).toggleClass("closed");
var btn = document.getElementById('searchsubmit');
btn.removeAttribute('type');
btn.setAttribute('type', 'submit');
});

工作

为什么要先删除该类型?你不能简单地替换/更改type属性吗?@Steven我这样做只是为了安全。如果你想的话,可以省略它。为什么要将DOM与jQuery混合使用?为什么不直接使用jQuery?使用jQuery attr函数不会覆盖它。。选中此项,类型仍然是
按钮
为什么不在按钮上设置一个“state”属性来跟踪当前步骤?第一次单击时,它会将状态设置为“提交”。第二次单击时,请执行表单.submit()。关于更改type属性的某些内容对我来说不太好。
jQuery("#submitsearch").click(function () {
jQuery("#searchtext").animate({
    width: 'toggle'
}, 500);
jQuery(this).toggleClass("closed");
var btn = document.getElementById('searchsubmit');
btn.removeAttribute('type');
btn.setAttribute('type', 'submit');
});