英文:
Multiple connected Radio input forms on single page
问题
我正在尝试在同一页上实现多个单选表单,并使它们相互通信。
所以,当您在页面顶部的表单A中选择一个单选值时,它应该在页面底部的表单B中显示相同的选择,反之亦然。
如何使这个工作?
我尝试了以下代码:
const $selected = $('.course-date input:checked')
if ($selected.length > 0) {
const date = $selected.val()
$("input:radio[name='course-date-2']").val(date).attr('checked', true);
}
希望这可以帮助您解决问题。
英文:
I am trying to implement multiple radio forms on a single page that communicate with each other.
So when you select a radio value in form A at the top of the page. It should display the same choice at the bottom on the page in form B. And vice versa.
How do i make this work?
i tried the following code:
const $selected = $('.course-date input:checked')
if ($selected.length > 0) {
const date = $selected.val()
$("input:radio[name='course-date-2']").val(date).attr('checked', true);
}
答案1
得分: 0
根据提供的信息,我认为您要查找的内容如下所示,如代码片段中所示。在这里,您需要使用 .prop()
将单选按钮设置为选中状态。
如果不是这个,请提供更多细节。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('input[type="radio"]').on('change', function() {
let selectedValue = $(this).val();
console.log('radio button changed to: ', selectedValue);
$('input[type="radio"][value=' + selectedValue + ']').prop('checked', true);
})
<!-- language: lang-css -->
form {
border: 1px solid;
padding: 2rem;
background: #f1f1f1;
font-family: sans-serif;
}
form + form {
margin-top: 2rem;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<h2>Form 1</h2>
<input type="radio" id="course-date-1" name="course-date" value="course1" checked>
<label for="course-date-1">course 1</label>
<input type="radio" id="course-date-2" name="course-date" value="course2">
<label for="course-date-2">course 2</label>
</form>
<form>
<h2>Form 2</h2>
<input type="radio" id="course-date-1A" name="course-date-A" value="course1" checked>
<label for="course-date-1A">course 1</label>
<input type="radio" id="course-date-2A" name="course-date-A" value="course2">
<label for="course-date-2A">course 2</label>
</form>
<!-- end snippet -->
请注意,这是您提供的代码片段的翻译部分。
英文:
Based on the info provided, I believe what you are looking for is the following as shown in the snippet.
Where you need to set the radio button to checked using .prop()
.
If this is not it, please provide more details.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('input[type="radio"]').on('change', function() {
let selectedValue = $(this).val();
console.log('radio button changed to: ', selectedValue);
$('input[type="radio"][value=' + selectedValue + ']').prop('checked', true);
})
<!-- language: lang-css -->
form {
border: 1px solid;
padding: 2rem;
background: #f1f1f1;
font-family: sans-serif;
}
form + form {
margin-top: 2rem;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<h2>Form 1</h2>
<input type="radio" id="course-date-1" name="course-date" value="course1" checked>
<label for="course-date-1">course 1</label>
<input type="radio" id="course-date-2" name="course-date" value="course2">
<label for="course-date-2">course 2</label>
</form>
<form>
<h2>Form 2</h2>
<input type="radio" id="course-date-1A" name="course-date-A" value="course1" checked>
<label for="course-date-1A">course 1</label>
<input type="radio" id="course-date-2A" name="course-date-A" value="course2">
<label for="course-date-2A">course 2</label>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论