如何在jquery中更改单选按钮时获取警报?

时间:2016-04-19 12:30:03

标签: javascript jquery

我有两个单选按钮和一个文本区域。现在我想做的是,当我在文本区域不为空时更改单选按钮时,我需要一个确认框。仅当用户单击“确定”时才必须更改单选按钮。否则,必须显示文本区域中的值,并且单选按钮的值不得更改。

我的代码

jQuery
--------

$(".radio1").change(function(event){

            if($(".txtarea").val() != "")
                { 
                    var flag = confirm("Do you want to change ?");
                }
            if(flag)
                {
                $(".txtarea").val("");
                }


        });


Html
------
<input type="radio" id="radio1" name="radio1" class="radio1">hello
<input type="radio" id="radio2" name="radio1" class="radio1">hi
<textarea id="txtarea" class="txtarea"></textarea>

5 个答案:

答案 0 :(得分:0)

试试这个:

$(document).ready(function() {
    $('input[type=radio][name=radiogroup]').change(function() {
        if (this.value == 'radio1') {
            alert("radio1 selected");
        }
        else if (this.value == 'radio2') {
            alert("radio2 selected");
        }
    });
});



<input type="radio" id="radio1" value="radio1" name="radiogroup" class="radio1">hello
<input type="radio" id="radio2" value="radio2" name="radiogroup" class="radio2">hi
<textarea id="txtarea" class="txtarea"></textarea>

答案 1 :(得分:0)

工作示例

&#13;
&#13;
public class School {
    private List<Department> departments;
    /*
     * All your methods and constructors goes here.
     */
}

public class Department {
    private List<Student> students;
    /*
     * All your methods and constructors goes here.
     */
}

public class Student {
    private String name;
    private String id;
    private String courseStream;
    private List<String> subjects;
    /*
     * All your methods and constructors goes here.
     */
}
&#13;
$(document).ready(function() {
    $('input[type=radio][name=radiogroup]').change(function() {
        if (this.value == 'radio1') {
           
          var r = confirm("Do you want to change ?");
         if (r == true) {
           
        $("#txtarea").val("Yes!");
           } else {
         $("#txtarea").val("No!");
          }
        }
        else if (this.value == 'radio2') {
            alert("radio2 selected");
          $("#txtarea").val("");
        }
    });
});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

根据您的要求,我创造了一个小提琴请看看

    jsfiddle.net/dhavalpankhaniya1989/4w536885/3/

如果您怀疑,请告诉我

答案 3 :(得分:0)

你需要这样的东西。

&#13;
&#13;
$(
  function()
  {
    $(".radio1").click(function(event)
	{
	  if(($(".txtarea").val()!="")&&(!$(this).attr('checked')))
	  { 
        if(confirm("Do you want to change ?"))
        {
          $(".txtarea").val("");
        }
        else
        {
          return(false);
        }
      }
    });
  }
)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" id="radio1" name="radio1" class="radio1"/><lable for="radio1">1</label>
<input type="radio" id="radio2" name="radio1" class="radio1"/><lable for="radio2">2</label>
<br/>
<textarea id="txtarea" class="txtarea"></textarea>
&#13;
&#13;
&#13;

注意 - 更改完成后, onchange 会触发,因此您无法使用它来阻止更改

答案 4 :(得分:0)

要从收音机中删除选择,只需在组中选择另一个收音机:

$(function() {
  $('input[name=radiogroup]').change(function() {
    if ($(this).attr("id") == "radio2") {
      if (confirm("Are you sure?")) $("textarea").val("");
      else $("#radio1").prop('checked', true);
    }
  });
});

和元素

<input type="radio" id="radio1" value="radio1" name="radiogroup" class="radio1" checked="checked">radio1
<input type="radio" id="radio2" value="radio2" name="radiogroup" class="radio2">radio2
<textarea id="textarea" class="txtarea"></textarea>

在这里试试:https://jsfiddle.net/ewnLw4wf/1/