隐藏HTML表单的提交按钮

时间:2012-07-17 15:24:15

标签: php javascript html

  

可能重复:
  Getting a form to submit when the enter key is pressed when there is no submit button

我正在尝试使用不可见的提交功能实现文本输入框。因此,该字段没有附加提交按钮,并在按下“enter”键时提交。

以下是我希望代码如何运作的示例:

<form method="get" action="<?php bloginfo('home'); ?>/"><p>

<input class="text_input" type="text" value="SEARCH: type &amp; hit Enter" name="s" id="s" onfocus="if 
(this.value == 'SEARCH: type &amp; hit Enter') {this.value = '';}" onblur="if (this.value == '') 
{this.value = 'SEARCH: type &amp; hit Enter';}" />

<input type="hidden" id="searchsubmit" value="" /></p></form>

这是我无法工作的代码:

<p>Have us call you. Input your number:</p>
<input type="text" id="phone" name="phone" maxlength="10" size="10">
<input type="submit" value="Click-to-Call"  onclick="window.open('http://secure.ifbyphone.com/clickto_status.php?click_id=34124&phone_to_call=' + getElementById('phone').value + '&key=asdfjknj3459dj38sdka92bva', 'Clickto' , 'width=200,height=200,toolbar=no,location=no, menubar=no, scrollbars=no, copyhistory=no,resizable=no')">

我尝试将带有GET请求的form属性添加到“onclick”部分中的URL无效。

有关如何使这项工作的任何想法?

2 个答案:

答案 0 :(得分:9)

将此问题添加到您的css有关ID searchsubmit:

    #searchsubmit
    {    
        visibility: hidden;
    }

该按钮仍然有效,但不可见。 如果表单的输入被聚焦/使用,则输入操作应该默认工作。


onBlur函数可能会触发您想要的操作。当用户离开输入字段(点击)时会发生onBlur

您可以尝试使用JQuery:

$(function() {
$('form').each(function() {
    $('input').keypress(function(e) {
        // Enter pressed?
        if(e.which == 10 || e.which == 13) {
            this.form.submit();
        }
    });

    $('input[type=submit]').hide();
});

});

这将触发表单的onSubmit事件,但您可以更改

this.form.submit

$('#phone').blur()

http://api.jquery.com/blur/


尝试使用以下方法添加javascript脚本块:

   document.getElementsById('phone')[0].onkeydown= function(event)
{
    if (event == undefined)
    {    
        event = window.event;
    }

    if (event.keyCode == 13)
    {
        var js_phone = document.getElementsByName('phone')[0].value;
        window.location = 'myPage.php?phone='+js_phone;
        return false;
    }
};

答案 1 :(得分:1)

要隐藏某些内容,您可以使用以下代码:

input[type=submit]
{
    display:hidden;
}

input[type=submit]仅作为示例,将其替换为选择器以提交按钮。

希望这有帮助。