简体   繁体   English

如何不在文本字段中显示文本

[英]How to NOT display text in a text field

I have a site here . 在这里有一个网站。

Here's the code from my little form... 这是我的小表格中的代码...

<form method="get" action="https://www.google.com/search" target="_blank"> 
    <table border="0" cellpadding="0"> 
        <tr>
            <td> 
                <input type="text" name="q" size="25" maxlength="50" value="" id="google" /> 
                <input type="submit" value="Search" />
            </td>
        </tr> 


        <tr>
            <td align="center" style="font-size:75%">
                <input type="radio" name="search" value="google.com" onclick="act('public library ')"/> Libraries
                <input type="radio" name="search" value="google.com" onclick="act('subway OR FedEx ')"/> Fax
                <!--<input type="radio" name="search" value="google.com" onclick="act('ice cream ')"/> Ice Cream-->                    
            </td>
        </tr>
    </table> 
</form>

Here's the Javascript related to the above HTML (maybe it has something to do with my question)... 这是与上述HTML相关的Javascript(也许与我的问题有关)...

function act(change) {
    myOption = change;
    document.getElementById("google").value = myOption;
    }

There's a JSFiddle here . 有一个的jsfiddle 这里 (Thanks to @koala_dev) (感谢@koala_dev)

On my site , if you click ' Libraries ', then " public library " will display in the text box. 在我的网站上 ,如果您单击“ ”,则“ 公共库 ”将显示在文本框中。 I left a space to enter the zip code, and then the results are displayed in a Google page in another window when Search is clicked. 我留了一个空格来输入邮政编码,然后单击“搜索”后,结果将显示在另一个窗口的Google页面中。

What I would like to see happen... 我想看到的发生...

I want the user to click ' Libraries ', but I don't want the text " public library " to display. 我希望用户单击“ ”,但不希望显示“ 公共库 ”文本。 I just want the user to enter their zip code and hit search. 我只希望用户输入他们的邮政编码并点击搜索。 I want the " public library " text to stay hidden. 我希望“ 公共图书馆 ”文本保持隐藏状态。

Edit: Sorry... The search field is the one on the top right-hand side of the page. 编辑:对不起...搜索字段是页面右上角的字段。 There's a label there that says Find it here! 那里有一个标签,上面写着 (Enter Zip Code) ... (输入邮政编码) ...

我想您想将功能绑定到“提交”按钮,并在其中将输入的ed值加上“ public library”或“ subway OR FedEx”

Here is my version 这是我的版本

Live Demo 现场演示

$(function() {
  $("form").on("submit",function(e) {
    var zip = $("#google").val();
    var what = $("input[name='what']:checked").val();
    what=what?zip+" "+what:zip;  
    if (what) $("#google").val(what);
    else e.preventDefault();  
  });
});    

You may want to reset the field by removing the radio value from it when focussing the field 您可能想通过在聚焦字段时从其中删除单选值来重置字段

Why not just use ajax to send your requests to google, then you could control exactly how things are handled in every event. 为什么不只使用ajax将请求发送给Google,然后您就可以精确控制每次事件中的处理方式。 It also would remove the need for a page refresh or pop-up window that could be blocked by pop-up blockers? 它还会消除对可能被弹出窗口阻止程序阻止的页面刷新或弹出窗口的需要吗?

$("input[name=search]").change(function () {
     //alert($(this).val());
     option = $(this).val();
     var $url = "https://www.google.com/search/" + option;
     $.ajax({
         url: $url,
         type: 'GET',
         beforeSend: function () {
             //do something before submitting
         },
         success: function (data, textStatus, xhr) {
             //success do something with the data you get back
             console.log(data);
             $("#results").html(data);
         },
         error: function (xhr, textStatus, errorThrown) {
             //something broke
             $("#results").html(errorThrown);
         }
     });
 });


    <form>
        <table border="0" cellpadding="0">
            <tr>
                <td>
                    <input type="text" name="q" size="25" maxlength="50" value="" id="google" />
                    <input type="submit" value="Search" />
                </td>
            </tr>
            <tr>
                <td align="center" style="font-size:75%">
                    <input type="radio" id='radiobtn1' name="search" value="Libraries" />Libraries
                    <input type="radio" id='radiobtn2' name="search" value="Fax" />Fax
                    <!--<input type="radio" name="search" value="google.com" onclick="act('ice cream ')"/>Ice Cream--></td>
            </tr>
        </table>
    </form>
    <div id="results"></div>

When you click on the the radio button search , it fires the javascript code act('public library ') . 当您单击单选按钮search ,它会触发javascript代码act('public library ') Looking through your code, you have a function act, below: 查看代码,您可以看到以下函数:

 function act(change) {
 myOption = change;
 document.getElementById("google").value = myOption;
} 

Your issue lies with the last part of that code document.getElementByID("google").value = myOption . 您的问题出在该代码的最后一部分document.getElementByID("google").value = myOption It doesn't seem like that javascript does anything else. 似乎javascript并没有执行其他任何操作。 Did you mean for it to do anything else afterwards? 您是说要事后再做其他事情吗? If not, you don't have to fire that javascript in the first place. 如果不是,则不必首先触发该javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM