javascript - 让按钮做某事

标签 javascript html button drop-down-menu

好吧,所以我有这个小的谷歌应用程序,我正在尝试将其组合在一起,并打算让它基于婴儿床笔记。有一个文本字段,其中有一个不起作用的按钮,上面写着“添加”、两个单选按钮和两个下拉菜单,每个下拉菜单用于一本书或一个故事。当我选中单选按钮、文本字段中的文本并单击“添加”时,我希望将文本发送到下拉菜单之一。
到目前为止,这是我的 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>

最佳答案

ADD 按钮添加一个 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>");
 }
})

关于javascript - 让按钮做某事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30992822/

相关文章:

grails - 验证 grails submitButton

php - 创建自定义 XML,然后通过 AJAX 获取该 XML

javascript - 如何选择合适数量的按钮

javascript - 在 JavaScript/JQuery 中获取调用者的 ID

javascript - Node.js Express 发送原始 HTTP 响应

javascript - Laravel - 使用 AJAX 过滤数据库的能力

javascript - 将 Plot.ly 与外部脚本结合使用来绘制图表

html - 按钮大小显示不正确

php css 如何根据成员更改名称颜色?

listview - 如何创建一个像在 flutter 中使用 listview 一样的按钮?