gpt4 book ai didi

javascript - 镜像多个选择字段 jquery/javascript

转载 作者:行者123 更新时间:2023-12-03 02:51:48 25 4
gpt4 key购买 nike

我是 JavaScript 和 HTML 新手,我想要实现以下结构:

[Drop down 1]
[Option 1]
[Option 2]
[Drop down 2]
[Option 1]
[Option 2]
..and so on

然后我希望选项反射(reflect)在下面的 TD 中,例如:

<td>&nbsp;</td><td class="sel1">Selected option from drop-down 1</td>
<td>&nbsp;</td><td class="sel2">Selected option from drop-down 2</td>
..and so on

更新:根据 Joshua Kisubi 的请求添加了 Jfiddle:https://jsfiddle.net/2ra5ptk1/1/

td {
font-family: calibri;
font-size: 14px;
border: 1px solid rgba(0, 0, 0, 0.2);
}

select,
body {
font-family: calibri;
font-size: 14px;
}
<body>
I need the selection from "Selection 1" to be outputted in the column to the right of "Results from selection 1" in the table below, and so on for each drop down. I don't know javascript or jQuery though, so I am not able to achieve this:<br><br>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
Selection 1:<br>
<select class="selection1">
<option value="choose">[Choose]</option>
<option value="option2">Sel1: Option 1</option>
<option value="option1">Sel1: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection2">
<option value="choose">[Choose]</option>
<option value="option2">Sel2: Option 1</option>
<option value="option1">Sel2: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection3">
<option value="choose">[Choose]</option>
<option value="option2">Sel3: Option 1</option>
<option value="option1">Sel3: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection4">
<option value="choose">[Choose]</option>
<option value="option2">Sel4: Option 1</option>
<option value="option1">Sel4: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection5">
<option value="choose">[Choose]</option>
<option value="option2">Sel5: Option 1</option>
<option value="option1">Sel5: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection6">
<option value="choose">[Choose]</option>
<option value="option2">Sel6: Option 1</option>
<option value="option1">Sel6: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection7">
<option value="choose">[Choose]</option>
<option value="option2">Sel7: Option 1</option>
<option value="option1">Sel7: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection8">
<option value="choose">[Choose]</option>
<option value="option2">Sel8: Option 1</option>
<option value="option1">Sel8: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection9">
<option value="choose">[Choose]</option>
<option value="option2">Sel9: Option 1</option>
<option value="option1">Sel9: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection10">
<option value="choose">[Choose]</option>
<option value="option2">Sel10: Option 1</option>
<option value="option1">Sel10: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection11">
<option value="choose">[Choose]</option>
<option value="option2">Sel11: Option 1</option>
<option value="option1">Sel11: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection12">
<option value="choose">[Choose]</option>
<option value="option2">Sel12: Option 1</option>
<option value="option1">Sel12: Option 2</option>
</select>
</td>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td>Result from selection 1:</td>
<td class="sel1">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 2:</td>
<td class="sel2">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 3:</td>
<td class="sel3">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 4:</td>
<td class="sel4">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 5:</td>
<td class="sel5">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 6:</td>
<td class="sel6">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 7:</td>
<td class="sel7">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 8:</td>
<td class="sel8">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 9:</td>
<td class="sel9">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 10:</td>
<td class="sel10">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 11:</td>
<td class="sel11">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 12:</td>
<td class="sel12">RESULT HERE</td>
</tr>
</table>
</td>
</tr>
</table>
</body>

任何人都可以给我任何关于如何实现此结果的提示或资源吗?

最佳答案

您在 html 中混淆了 id 和 class 的用法,因此很难在 javascript 中引用。使用 id 表示页面上的唯一元素,使用类表示相似的元素。这样您就可以立即附加一个事件处理程序来选择元素。当然,您可以使用 for 循环附加到 id 'sel'+ count 的选择,但这效率低下。

$(function() {
$('.selection').on('change', function(){
var sel_id = $(this).attr('data-id');
var sel_opt = $(this).val();

$('table td#'+sel_id).text(sel_opt)
})
});
td {
font-family:calibri;
font-size:14px;
border:1px solid rgba(0,0,0,0.2);
}

select, body {
font-family:calibri;
font-size:14px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
I need the selection from "Selection 1" to be outputted in the column to the right of "Results from selection 1" in the table below, and so on for each drop down. I don't know javascript or jQuery though, so I am not able to achieve this:<br><br>
<table cellpadding="0" cellspacing="0">
<tr>
<td>

Selection 1:<br>
<select class="selection" data-id="sel1">
<option value="choose">[Choose]</option>
<option value="option2">Sel1: Option 1</option>
<option value="option1">Sel1: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel2">
<option value="choose">[Choose]</option>
<option value="option2">Sel2: Option 1</option>
<option value="option1">Sel2: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel3">
<option value="choose">[Choose]</option>
<option value="option2">Sel3: Option 1</option>
<option value="option1">Sel3: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel4">
<option value="choose">[Choose]</option>
<option value="option2">Sel4: Option 1</option>
<option value="option1">Sel4: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel5">
<option value="choose">[Choose]</option>
<option value="option2">Sel5: Option 1</option>
<option value="option1">Sel5: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel6">
<option value="choose">[Choose]</option>
<option value="option2">Sel6: Option 1</option>
<option value="option1">Sel6: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel7">
<option value="choose">[Choose]</option>
<option value="option2">Sel7: Option 1</option>
<option value="option1">Sel7: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel8">
<option value="choose">[Choose]</option>
<option value="option2">Sel8: Option 1</option>
<option value="option1">Sel8: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel9">
<option value="choose">[Choose]</option>
<option value="option2">Sel9: Option 1</option>
<option value="option1">Sel9: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel10">
<option value="choose">[Choose]</option>
<option value="option2">Sel10: Option 1</option>
<option value="option1">Sel10: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel11">
<option value="choose">[Choose]</option>
<option value="option2">Sel11: Option 1</option>
<option value="option1">Sel11: Option 2</option>
</select>
<br><br>Selection 1:<br>
<select class="selection" data-id="sel12">
<option value="choose">[Choose]</option>
<option value="option2">Sel12: Option 1</option>
<option value="option1">Sel12: Option 2</option>
</select>

</td><td>

<table cellpadding="0" cellspacing="0">
<tr>
<td>Result from selection 1:</td><td id="sel1">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 2:</td><td id="sel2">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 3:</td><td id="sel3">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 4:</td><td id="sel4">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 5:</td><td id="sel5">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 6:</td><td id="sel6">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 7:</td><td id="sel7">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 8:</td><td id="sel8">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 9:</td><td id="sel9">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 10:</td><td id="sel10">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 11:</td><td id="sel11">RESULT HERE</td>
</tr>
<tr>
<td>Result from selection 12:</td><td id="sel12">RESULT HERE</td>
</tr>
</table>

</td>
</tr>
</table>

</body>

关于javascript - 镜像多个选择字段 jquery/javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47818327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com