Multiple connected Radio input forms on single page

huangapple go评论58阅读模式
英文:

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 -->

$(&#39;input[type=&quot;radio&quot;]&#39;).on(&#39;change&#39;, function() {
  let selectedValue = $(this).val();
  console.log(&#39;radio button changed to: &#39;, selectedValue);
  $(&#39;input[type=&quot;radio&quot;][value=&#39; + selectedValue + &#39;]&#39;).prop(&#39;checked&#39;, true);
})

<!-- language: lang-css -->

form {
  border: 1px solid;
  padding: 2rem;
  background: #f1f1f1;
  font-family: sans-serif;
}

form + form {
  margin-top: 2rem;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;form&gt;
  &lt;h2&gt;Form 1&lt;/h2&gt;
  &lt;input type=&quot;radio&quot; id=&quot;course-date-1&quot; name=&quot;course-date&quot; value=&quot;course1&quot; checked&gt;
  &lt;label for=&quot;course-date-1&quot;&gt;course 1&lt;/label&gt;
  &lt;input type=&quot;radio&quot; id=&quot;course-date-2&quot; name=&quot;course-date&quot; value=&quot;course2&quot;&gt;
  &lt;label for=&quot;course-date-2&quot;&gt;course 2&lt;/label&gt;
&lt;/form&gt;

&lt;form&gt;
  &lt;h2&gt;Form 2&lt;/h2&gt;
  &lt;input type=&quot;radio&quot; id=&quot;course-date-1A&quot; name=&quot;course-date-A&quot; value=&quot;course1&quot; checked&gt;
  &lt;label for=&quot;course-date-1A&quot;&gt;course 1&lt;/label&gt;
  &lt;input type=&quot;radio&quot; id=&quot;course-date-2A&quot; name=&quot;course-date-A&quot; value=&quot;course2&quot;&gt;
  &lt;label for=&quot;course-date-2A&quot;&gt;course 2&lt;/label&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 18:11:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定