【问题标题】:Making a button do something让按钮做某事
【发布时间】:2015-06-23 02:19:27
【问题描述】:

好的,所以我有这个小谷歌应用程序,我正在尝试将它放在一起,并将让它基于婴儿床笔记。有一个文本字段,其中包含一个不起作用的按钮,上面写着“添加”、两个单选按钮和两个下拉菜单,每个下拉菜单分别用于书籍或故事。当我选中一个单选按钮时,在我的文本字段中输入文本,然后单击添加,我希望将文本发送到下拉菜单之一。
到目前为止,这是我的 HTML

    <h1>Crib Reader</h1>

<table align="center">
    <tr>
        <td style="width: 134px; ">
            <input type="radio" name="Reader" value="Book" style="vertical-align: middle" checked="checked">Book
            <input type="radio" name="Reader" value="Story" style="vertical-align: middle">Story
        </td>
        <td style="width: 162px; "><input type="text" name="Name" style="height: 20px;"></td>
        <td> <button type="button">Add</button> </td>
    </tr>
</table>
<br></br>
<table align="center" style="width: 316px; ">
    <tr>
        <td style="width: 115px; ">
            Books<br></br>
            <select id="Books" style="width: 100px; ">
                <option value=""></option>
            </select>
        </td>
        <td>
            <button type="button">Load</button>
        </td>

        <td style="width: 115px; ">
            Stories<br></br>
            <select id="Stories" style="width: 100px; ">
                <option value=""></option>
            </select>
        </td>
                    <td>
            <button type="button">Load</button>
        </td>
    </tr>
</table>
<hr>


  </body>
</html>

【问题讨论】:

  • 这篇文章真的令人困惑,直到有人修复了格式,以后请多加小心。

标签: javascript html button drop-down-menu


【解决方案1】:

添加按钮添加一个id: HTML:

<table align="center">
    <tr>
        <td style="width: 134px; ">
            <input type="radio" name="Reader" value="Book" style="vertical-align: middle" checked="checked">Book
            <input type="radio" name="Reader" value="Story" style="vertical-align: middle">Story
        </td>
        <td style="width: 162px; "><input type="text" name="Name" style="height: 20px;"></td>
        <td> <button type="button" id="add">Add</button> </td>
    </tr>
</table>

Js:

捕获点击事件并检查哪个单选按钮被选中并相应地在选择选项中添加值

$("#add").on("click",function(){
 if($('input[name="Reader"]:checked').val() == "Book"){
  $("#Books").append("<option>"+$("input[name='Name']").val()+"</option>");
  }else{
  $("#Stories").append("<option>"+$("input[name='Name']").val()+"</option>");
 }
})

【讨论】:

  • 这个我明白,但是我把这段代码放在哪里,我好像找不到Java区让这段代码真正起作用。
  • Java区是什么意思?在 javascript 中,将我的 js 保存在 document.ready 函数中
猜你喜欢
  • 2020-07-01
  • 1970-01-01
  • 2019-07-13
  • 2013-05-04
  • 2012-10-24
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多