jquery单选按钮单击并更改功能不起作用

时间:2022-11-20 14:16:38

I want to hide and show div according to radio buttons value. HTML code is,

我想根据单选按钮值隐藏和显示div。 HTML代码是,

<div id="share_to_others">
  <input type="radio" value="33" id="fx_sharepl_type" name="fx_sharepl_type">
  <input type="radio" value="22" id="fx_sharepl_type" name="fx_sharepl_type">
  <input type="radio" value="11" id="fx_sharepl_type" name="fx_sharepl_type">
</div>

And jquery code that i tried is,

我试过的jquery代码是,

$("#share_to_others input[name='fx_sharepl_type']").click(function () {
    alert("test");
});

$("#share_to_others input[name='fx_sharepl_type']").change(function () {
    alert("test");
});

$("#fx_sharepl_type").change(function () {
    alert("asdas");
});

$("input[name=fx_sharepl_type]:radio").change(function () {
    alert("click fired");   
});

$(document).on('change', 'input:radio[name=fx_sharepl_type"]', function (event) {
    alert("click fired");
});

Many of them from jsfiddle working demo, But not working for me, i dont know why. Am i doing anything wrong?

他们中的许多人来自jsfiddle工作演示,但不适合我,我不知道为什么。我做错了吗?

3 个解决方案

#1


0  

$(function() { // DOM loaded event handler
  var show_duration = 0;
  var content_33 = $("#content_33");
  var content_22 = $("#content_22");
  var content_11 = $("#content_11");
  var bloc_radio_share = $("#share_to_others");
  
  // Take an html element in parameter and show it
  function show_content(content_id) {
    content_id.show(show_duration); 
  }
  
  // Take an html element in parameter and hide it
  function hide_content(content_id) {
    content_id.hide(0); 
  }
  
  hide_content(content_22);
  hide_content(content_11);
  
  
  bloc_radio_share.change(function() {
    var radio_checked_val = $('input[name=fx_sharepl_type]:checked', '#share_to_others').val();
    
    if (radio_checked_val == 33) {
      hide_content(content_22);
      hide_content(content_11);
      show_content(content_33);
    }
    else if (radio_checked_val == 22) {
      hide_content(content_33);
      hide_content(content_11);
      show_content(content_22);
    }
    else { // case content == 11
      hide_content(content_33);
      hide_content(content_22);
      show_content(content_11);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="share_to_others">
  <label>
    <input type="radio" value="33" id="fx_sharepl_type" name="fx_sharepl_type" checked />
    33
   </label>
  <label>
    <input type="radio" value="22" id="fx_sharepl_type" name="fx_sharepl_type" />
    22
  </label>
  <label>
    <input type="radio" value="11" id="fx_sharepl_type" name="fx_sharepl_type" />
    11
  </label>
</div>
<div id="content_33">
  This div is displayed because of click on radio button 33
</div>
<div id="content_22">
  Radio button 22 has been clicked so I appear
</div>
<div id="content_11">
  If you click on radio button 11 you'll see me, like now
</div>

Here is an example of algorithm that fits your need. Logic may be not the best or the fastest but that is a begin.

以下是适合您需求的算法示例。逻辑可能不是最好的或最快的,但这是一个开始。

#2


2  

You have to give unique id to each radio button then after do like this way.

你必须为每个单选按钮提供唯一的ID,然后这样做。

$("#r1, #r2, #r3").change(function () {

#3


0  

jquery单选按钮单击并更改功能不起作用Your code wors but you forgot to include a JQuery source version located to the left side of the JSFiddle window. Selecting any version of JQuery will make your code work.

您的代码很糟糕,但您忘记在JSFiddle窗口的左侧包含一个JQuery源代码版本。选择任何版本的JQuery都可以使您的代码正常工作。

#1


0  

$(function() { // DOM loaded event handler
  var show_duration = 0;
  var content_33 = $("#content_33");
  var content_22 = $("#content_22");
  var content_11 = $("#content_11");
  var bloc_radio_share = $("#share_to_others");
  
  // Take an html element in parameter and show it
  function show_content(content_id) {
    content_id.show(show_duration); 
  }
  
  // Take an html element in parameter and hide it
  function hide_content(content_id) {
    content_id.hide(0); 
  }
  
  hide_content(content_22);
  hide_content(content_11);
  
  
  bloc_radio_share.change(function() {
    var radio_checked_val = $('input[name=fx_sharepl_type]:checked', '#share_to_others').val();
    
    if (radio_checked_val == 33) {
      hide_content(content_22);
      hide_content(content_11);
      show_content(content_33);
    }
    else if (radio_checked_val == 22) {
      hide_content(content_33);
      hide_content(content_11);
      show_content(content_22);
    }
    else { // case content == 11
      hide_content(content_33);
      hide_content(content_22);
      show_content(content_11);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="share_to_others">
  <label>
    <input type="radio" value="33" id="fx_sharepl_type" name="fx_sharepl_type" checked />
    33
   </label>
  <label>
    <input type="radio" value="22" id="fx_sharepl_type" name="fx_sharepl_type" />
    22
  </label>
  <label>
    <input type="radio" value="11" id="fx_sharepl_type" name="fx_sharepl_type" />
    11
  </label>
</div>
<div id="content_33">
  This div is displayed because of click on radio button 33
</div>
<div id="content_22">
  Radio button 22 has been clicked so I appear
</div>
<div id="content_11">
  If you click on radio button 11 you'll see me, like now
</div>

Here is an example of algorithm that fits your need. Logic may be not the best or the fastest but that is a begin.

以下是适合您需求的算法示例。逻辑可能不是最好的或最快的,但这是一个开始。

#2


2  

You have to give unique id to each radio button then after do like this way.

你必须为每个单选按钮提供唯一的ID,然后这样做。

$("#r1, #r2, #r3").change(function () {

#3


0  

jquery单选按钮单击并更改功能不起作用Your code wors but you forgot to include a JQuery source version located to the left side of the JSFiddle window. Selecting any version of JQuery will make your code work.

您的代码很糟糕,但您忘记在JSFiddle窗口的左侧包含一个JQuery源代码版本。选择任何版本的JQuery都可以使您的代码正常工作。