将按钮链接到输入

Link button to input

本文关键字:输入 链接 按钮      更新时间:2023-09-26

我正在尝试将输入链接到按钮。从某种意义上说,按钮将触发输入,并且输入将保持隐藏状态。

以下是我找到的解决方案:

<a href="javascript:void(0)" id="files" href=""> 
 <button id="uploadDevice" class="button button-block button-positive">
            <i class="icon ion-android-mail"></i> &nbsp; <text id="textBtn1">From Device </text>
</button></a>

脚本:

<script>
  $("#files").click(function(){
        $(this).next().trigger('click');
    });
    </script>

CSS

 <style>
  .uploadDevice{
     visibility : hidden;
  }

我的问题如下:按钮和输入必须具有不同的ID。上述解决方案有效,但是当我需要它们不同时,按钮和输入 ID 必须相同。

像这样的东西?

.HTML:

<input type="hidden" id="typeHidden" />
<input type="button" id="mybutt" value="Show Me" />

javascript/jQuery:

$('#mybutt').click(function(){
    $('#typeHidden').val('Secret text');
});

jsFiddle 演示


如果要有多个 DIV/按钮组合,并且希望按钮更新相应的 DIV,请参阅

此修订版 JS小提琴