点击清除内容

发布于 2024-08-22 08:37:51 字数 505 浏览 8 评论 0原文

在以下代码中:

<INPUT TYPE="radio" name="1" id="1" VALUE="1" <?php echo $checked1 ?>><font size="2">1</font>
<INPUT TYPE="radio" name="2" id="2" VALUE="2" <?php echo $checked2 ?>><font size="2">2</font>
<TEXTAREA name="names" id="names" rows="15" cols="65"><?php echo $names ?></TEXTAREA>

如果第一次选择单选按钮 1,则文本区域上的 onclick 其内容应被清除,但如果用户第二次单击同一文本区域,则内容不应被清除被清除为相同的单选按钮1。

这同样适用于单选按钮 2。这是怎么做到的?

In the following code:

<INPUT TYPE="radio" name="1" id="1" VALUE="1" <?php echo $checked1 ?>><font size="2">1</font>
<INPUT TYPE="radio" name="2" id="2" VALUE="2" <?php echo $checked2 ?>><font size="2">2</font>
<TEXTAREA name="names" id="names" rows="15" cols="65"><?php echo $names ?></TEXTAREA>

If the radio button 1 selected for the first time then onclick on textarea its contents should be cleared, but if the user clicks for the second time on the same text area the contents should not be cleared for the same radio button1.

The same should hold good for radio button2. How is this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静赏你的温柔 2024-08-29 08:37:51

这将满足您的要求。只需放置在 body 标签中

<input type="radio" name="G1"  id="1" value="1" /> <font size="2">1</font>
<input type="radio" name="G1" id="2" value="2"  /> <font size="2">2</font>
<textarea name="names" id="names" rows="15" cols="65" onfocus="handleOnFocus()"></textarea>

<script type="text/javascript">
    var isCleardForButton1 = false;
    var isCleardForButton2 = false;

    function handleOnFocus() {
        var objTA = document.getElementById("names");
        var objRadio1 = document.getElementById("1");
        var objRadio2 = document.getElementById("2");
        if (isCleardForButton1 == false && objRadio1.checked == true) {
            objTA.value = "";
            isCleardForButton1 = true;
        }
        if (isCleardForButton2 == false && objRadio2.checked == true) {
            objTA.value = "";
            isCleardForButton2 = true;
        }
    }
</script>

This will fullfill your requirement. just place in body tag

<input type="radio" name="G1"  id="1" value="1" /> <font size="2">1</font>
<input type="radio" name="G1" id="2" value="2"  /> <font size="2">2</font>
<textarea name="names" id="names" rows="15" cols="65" onfocus="handleOnFocus()"></textarea>

<script type="text/javascript">
    var isCleardForButton1 = false;
    var isCleardForButton2 = false;

    function handleOnFocus() {
        var objTA = document.getElementById("names");
        var objRadio1 = document.getElementById("1");
        var objRadio2 = document.getElementById("2");
        if (isCleardForButton1 == false && objRadio1.checked == true) {
            objTA.value = "";
            isCleardForButton1 = true;
        }
        if (isCleardForButton2 == false && objRadio2.checked == true) {
            objTA.value = "";
            isCleardForButton2 = true;
        }
    }
</script>
一瞬间的火花 2024-08-29 08:37:51

演示,这是代码:

<TEXTAREA name="names" id="names" rows="15" cols="65" onclick="doIt(this);">Hello There</textarea>

<script>
var isDone = false;

function doIt(field)
{
  if (document.getElementById("1").checked == true && isDone == false)
  {
     field.value = "";
     isDone = true;
  }
}
</script>

这将第一次清除内容,而不是再次为页面的生命周期。

Demo and here is the code:

<TEXTAREA name="names" id="names" rows="15" cols="65" onclick="doIt(this);">Hello There</textarea>

<script>
var isDone = false;

function doIt(field)
{
  if (document.getElementById("1").checked == true && isDone == false)
  {
     field.value = "";
     isDone = true;
  }
}
</script>

This would clear contents for the first time and not again for the life time of page.

极致的悲 2024-08-29 08:37:51

编辑:看来我误解了你的要求。这仅适用于单击单选按钮,不适用于文本区域。

您需要设置一个点击事件来清除textarea内容,然后自行解除绑定。

使用 jQuery,事件处理程序将类似于:

$('#names').val('');
$(this).unbind('click');

edit: Looks like I mis-understood your requirements. This only applies to the click of the radio button, not to the textarea.

You need to setup a click event to clear the textarea content, and then unbind itself.

With jQuery the event handler would be something like:

$('#names').val('');
$(this).unbind('click');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文