You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jquery实现文本框内容切换(Yes/空/No)需求求助

Fixing the Toggle Functionality for Your Button

Hey there! I see you're trying to make a button that toggles the text box content instead of appending "yes" every time—let's get this sorted out for you. Since you can't use radio/checkboxes due to system bugs, we'll handle this entirely with jQuery logic.

Solution 1: Toggle between "yes" and empty text

This will set the text box to "yes" on the first click, then clear it on the second, and repeat the cycle:

<table>
<tr><td class="formCell"><input type="text" value="" id="text" style="width: 100%;" /></td></tr>
<!--BUTTON START-->
<tr><td class="formCell"><input type="button" name="yesOrNo" class="checkBox" id="button"></td></tr>
<!--BUTTON END-->
</table>
<script type="text/javascript">
$(function () {
  $('#button').on('click', function () {
    var textInput = $('#text');
    // Check current value of the text box
    if (textInput.val() === 'yes') {
      // If it's already "yes", clear it
      textInput.val('');
    } else {
      // Otherwise, set it to "yes"
      textInput.val('yes');
    }
  });
});
</script>

Solution 2: Toggle between "yes" and "no"

If you'd prefer switching between "yes" and "no" instead of clearing, use this version:

$(function () {
  $('#button').on('click', function () {
    var textInput = $('#text');
    var currentValue = textInput.val();
    if (currentValue === 'yes') {
      textInput.val('no');
    } else {
      // If it's "no" or empty, set to "yes"
      textInput.val('yes');
    }
  });
});

How this works

  • We first grab the text box element using $('#text') and store it in a variable for efficiency.
  • We check the current value of the text box with .val().
  • Based on the current value, we either set it to the opposite state (empty/yes or no/yes) using .val(newValue).

This logic avoids appending text and gives you the toggle behavior you need, no radio buttons required!

内容的提问来源于stack exchange,提问作者user4385322

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:13:10