开发者

How can I Bind one Selector and event to a radio button

I am Editing this question Clearly I have "radio2" checked as default on loading the document. After that the user can select "radio1".

I have two parts of code:

Handle when checked

开发者_如何学JAVA
if($("#radio2:checked") && $("#somehidden").val()==="ABC") {
     //Do some Set of actions 
    $("#txt1").val("BCD");
    $("#txt2").val("ZXY");
}

Handle When Clicked

$("#radio2").bind('click',function(){
  if($("#somehidden").val()==="ABC") {
    $("#txt1").val("BCD");
    $("#txt2").val("ZXY");
  }     
});

I have written the first part of the code for on load the document I have same set of actions and these actions appear the same when we click that we have seen in the second part of the code. My question is I have to remove the First part of code why because as I already have the hidden value condition on the click. How can I handle both? I am using trigger() to select it. I don't want to do that.


Use trigger to trigger the event.

$("#radio1").bind('click',function(){
    alert("Checked");
}).trigger('click'); //on ready;


Try this:

$(function() {
  $("#radio1").bind('click',function(){
    alert("Checked");
  });

  $("#radio1").click(); // on ready
});


$(function() {
  var $radio1 = $("#radio1"),
      actionIfChecked = function() {
        alert("Checked");
      };

  $(window).load(function() {
    if ($radio1.is(":checked")) actionIfChecked();
  });

  $radio1.click(actionIfChecked);
});

Edit: Its possible I misinterpreted the question, judging by the other responses.


$(function() {
  $("#radio1").bind('click',function(){
    if ($(this).is(":checked") {
       // Do your stuff
    }
  })
});


I'm not sure if this is what you're looking for but it might help:

if($("#radio1").attr("checked")){
    alert("Checked");
}

$(function() {
    $("#radio1").bind('click',function(){
        if(this.checked){
            alert("Checked");
        }
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜