使用Jquery更改要检查或未选中的单选按钮

时间:2018-02-01 07:46:39

标签: javascript jquery html css

这是我的单选按钮



public class AstarAI : MonoBehaviour
{

    //The point to move to
    public Transform target;

    private Seeker seeker;

    //The calculated path
    public Path path;

    //The AI's speed per second
    public float speed = 2;

    //The max distance from the AI to a waypoint for it to continue to the next waypoint
    public float nextWaypointDistance = 3;

    //The waypoint we are currently moving towards
    public int currentWaypoint = 0;


    public bool arrived;

    public void Start()
    {
       seeker = GetComponent<Seeker>();
       arrived = false;    
       seeker.StartPath(transform.position, target.position, OnPathComplete);

    }



    public void OnPathComplete(Path p)
    {
        Debug.Log("Path calculated? " + p.error);
        if (!p.error)
        {
            path = p;
            //Reset the waypoint counter
            currentWaypoint = 0;
        }
    }

    public void FixedUpdate()
    {


        if (path == null)
        {
            //We have no path to move after yet
            return;
        }

        if (currentWaypoint >= path.vectorPath.Count)
        {
            Debug.Log("End Of Path Reached");
            arrived = true;
            return;
        }

        //Direction to the next waypoint
        Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
        dir *= speed * Time.fixedDeltaTime;
        this.gameObject.transform.Translate(dir);

        //Check if we are close enough to the next waypoint

        if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance)
        {
            currentWaypoint++;
            return;
        }
    }
}
&#13;
$(".stayalert_checkbox").click(function(){
  alert($(this).val());
  $("#stay_alert_div").toggle();
});
&#13;
&#13;
&#13;

此处点击单选按钮Div正在切换。所以

我想在div隐藏时取消选中收音机。我知道可以选择使用复选框,但可以使用无线电。

当我试图获得无线电的价值时,它会返回&#34; ON&#34;

我试过

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="stay_alert_checkbox" class="stayalert_checkbox"> 

<div id="stay_alert_div" style="display:none">
  <h1>I Am in Div</h1>
</div>

3 个答案:

答案 0 :(得分:1)

您可以使用标志变量

Stack Snippet

&#13;
&#13;
var isChecked = 0;
$(".stayalert_checkbox").on('click', function() {
  if (isChecked) {
    $("#stay_alert_div").hide();
    $(this).prop('checked', false);
    isChecked = 0;
  } else {
    $("#stay_alert_div").show();
    $(this).prop('checked', true);
    isChecked = 1;
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="stay_alert_checkbox" class="stayalert_checkbox">

<div id="stay_alert_div" style="display:none">
  <h1>I Am in Div</h1>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

一种简单的方法是使用它:

  if($("#stay_alert_div").is(":visible")){
    $(this).prop("checked",false);
  }

<强>演示

&#13;
&#13;
$(".stayalert_checkbox").click(function(){
  if($("#stay_alert_div").is(":visible")){
    $(this).prop("checked",false);
  }
  $("#stay_alert_div").toggle();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="stay_alert_checkbox" class="stayalert_checkbox"> 

<div id="stay_alert_div" style="display:none">
  <h1>I Am in Div</h1>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我不太确定

static void Main(string[] args)
    {
        for (int a = 0; a < 5; a++)
        {
            var client = new TcpClient("localhost", 2000);
            Console.WriteLine(client.Connected);
            client.close();
        }
    }
$(".stayalert_checkbox").click(function(){
if($(this).attr('checked')){
 $(this).attr('checked',false).prop('value','off');    
 }
 else{
 $(this).attr('checked',true).prop('value','on');     
 }
  alert($(this).val());
 $("#stay_alert_div").toggle();
});