javascript - 使用javascript单击相应表行单选按钮时如何获取选择框值?

标签 javascript jquery html

使用javascript单击相应表格行单选按钮时如何获取选择框值?

我有 3 个单选按钮,当选择其中一个时,我想自动获取相应的行选择框 toyname 到文本框 toynamelpd

<input type="text" value="" name="toynamelpd" id="toynamelpd">
<table class="table table-condensed table-striped table-bordered" style="width: 100%;" ng-if="lessonPlanDailys.length > 0">
    <thead style="height: 60px; overflow: auto;">
        <tr>
        <th style="text-align: center;color: teal;width: 5%;padding: 1%;"></th>
        <th style="text-align: center;color: teal;width: 5%;padding: 1%;">#</th>
        <th style="color: teal;width: 15%;text-align: center;padding: 1%;">Toys</th>
        </tr>
    </thead>
<tbody>
    <%
    int index = 1;
    for (Map<String, Object> eachtoyPlanDaily : toyPlanDailys) 
    {
    %>
    <tr>
        <td style="text-align: center;width: 5%; "><input type="radio" id="toyPlanId" name="toyPlanId" value="<%=eachtoyPlanDaily.get("_id").toString()%>" onchange="selecttpd()"></td>
    <td style="text-align: center;width: 5%; "><%=index++%></td>
    <td style="width: 15%;text-align: justify; ">
            <select name="toynamelp" id="toynamelp" value="this.value">
            <option value="select">-------------select-------------</option>
            <%
            Iterator iterator = toynameList.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry mapEntry = (Map.Entry) iterator.next();
                if(eachtoyPlanDaily.get("toyname") != null && eachtoyPlanDaily.get("toyname").equals(mapEntry.getValue())){
                %>
                    <option selected value="<%=mapEntry.getKey()%>"><%=mapEntry.getValue()%></option>
                <%
                }
                else {
                %>
                    <option value="<%=mapEntry.getKey()%>"><%=mapEntry.getValue()%></option>
                <% 
                }
            }
            %>
        </select>
    </td>
    </tr>
   <%
   }
   %>
</tbody>
</table>

最佳答案

试试这个代码。希望它能起作用..

HTML:

<tr>
    <td>
        <select id="select-td-1">
            <option>Value 11</option>
            <option>value 12</option>
        </select>
    </td>
    <td> 
        <input class="clicked" type="radio" data-attr="td-1">
    </td>
</tr>
<tr>
    <td>
        <select id="select-td-2">
            <option>Value 21</option>
            <option>value 22</option>
        </select>
    </td>
    <td> 
        <input class="clicked" type="radio" data-attr="td-2">
    </td>
</tr>
<tr>
    <td>
        <select id="select-td-3">
            <option>Value 31</option>
            <option>value 32</option>
        </select>
    </td>
    <td> 
        <input class="clicked" type="radio" data-attr="td-3">
    </td>
</tr>

JQuery:

$(document).ready(function(){
    $('.clicked').on('click', function(){
        var attr = $(this).attr('data-attr');
        var selectval = $("#select-"+attr).val();
        console.log(selectval);
    });
});

关于javascript - 使用javascript单击相应表行单选按钮时如何获取选择框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42069728/

相关文章:

javascript - 当查询字符串改变时更新 react 组件

javascript - 学习 API : cant read property of forEach loop

javascript - 如何防止 dom 更改在 ajax 请求时重置

javascript - 将多个元素链接在一起,使它们共享相同的内容

Python BeautifulSoup - 在嵌套的 <div> 和 <ul> 中查找特定的 <li>

javascript - Backbone.js:model.isNew() == true,但 model.save() 导致 POST 请求而不是 PUT

javascript - 无法读取 AngularJS $resource DELETE 的响应

jQuery 模式对话框导致 IE 滚动条出现

html - 如何让文本居中以及如何让 <DIV> 成为背景图片?

jquery - 子导航下拉问题