两个按钮,可随背景位置变化而切换,并在输入中显示结果

Two buttons that toggle with background-position changes with a result in an Input

本文关键字:结果 显示 输入 变化 位置 两个 按钮 背景      更新时间:2023-09-26

如何创建一个jQuery/javascript函数来:

  1. 将其值插入输入框(男性/女性)
  2. 更改其背景及其同级按钮的背景以显示它是否处于活动状态?

这是我的 HTML 开始:

<input type="button" class="ts" id="female" onclick="???????????" />
<input type="button" class="ts" id="male"   onclick="???????????" />
<input class="req-string bx long" type="text" id="gender" name="gender" />

这里有一个例子,它将引导你走上正确的道路。它没有按照你的要求去做,但你会明白的。

下面的代码将禁用另一个按钮:如果您单击"女性",它将禁用"男性",反之亦然。

$(function() {
  $('#female').click(function() {     
    // place text ("male" or "female") in an input 
    $('#gender').val('female');
    $('#female').css({background:'green'});
    $('#male').css({background:'red'});
  });
  $('#male').click(function() {      
    // place text ("male" or "female") in an input 
    $('#gender').val('male');
    // emulates the button being toggled
    $('#female').css({background:'red'});
    $('#male').css({background:'green'});
  });
});​