陈一文478 2023-10-12 08:49 采纳率: 100%
浏览 5
已结题

如何新增一个珍藏勾选框,点击则到输入框种

如何新增一个珍藏勾选框,与原创的复选框功能相同,点击则到输入框种


<input name="oTitle" type="text" class="css_input" id="oTitle" value="<%=U.vTitle%>" size="60" <%if len(u.vtitle)=0 and lcl_titles=1 then%>onblur="ChkTitle('');"<%end if%><%if len(u.vtitle)=0 and lcl_titles=2 then%>onblur="ChkTitle($('oCid').value);"<%End If%> />
<label>
<input name="oCommend" type="checkbox" value="1" <%If U.vCommend = 1 Then Response.Write "checked='checked'"%> />原创
</label>
<script>
    var checkBox = document.querySelector('input[name="oCommend"]');
    var inputBox = document.querySelector('input[name="oTitle"]');
    checkBox.addEventListener('change', function() {
        if (this.checked) {
            inputBox.value += '【原创】';
        } else {
            inputBox.value = inputBox.value.replace('【原创】', '');
        }
    });
</script>
<label><input name="oCommend" type="checkbox" value="1" <%If U.vCommend = 1 Then Response.Write "checked='checked'"%> /> 推荐</label>
<label><input name="oIsComment" type="checkbox" value="1" <%If U.vIsComment = 1 Then Response.Write "checked='checked'"%> /> 评论</label>
<label><input name="oDisplay" type="checkbox" value="1" <%If U.vDisplay = 1 Then Response.Write "checked='checked'"%> /> 发布</label>
  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-12 11:29
    关注

    【以下回答由 GPT 生成】

    首先,你已经有了一个可行的实现方案。你的HTML代码中已经包含了一个勾选框和一个输入框,并且你的JavaScript代码也可以实现在点击勾选框时在输入框中添加内容的功能。

    不过,我注意到你的JavaScript代码使用了querySelector方法来获取元素。querySelector方法返回的是匹配指定选择器的第一个元素,这在你的情况下是有效的。但如果你需要在页面中有多个勾选框和输入框时,querySelector只会返回第一个匹配的元素。如果你想对多个元素应用相同的功能,可以考虑使用querySelectorAll方法,并在遍历元素时应用相同的代码。

    如果你想要在页面中新增一个珍藏勾选框,你可以使用以下HTML代码:

    <input name="oTitle" type="text" class="css_input" id="oTitle" value="<%=U.vTitle%>" size="60" <%if len(u.vtitle)=0 and lcl_titles=1 then%>onblur="ChkTitle('');"<%end if%> <%if len(u.vtitle)=0 and lcl_titles=2 then%>onblur="ChkTitle($('#oCid').value);"<%End If%> />
    
    <label>
      <input name="oCommend" type="checkbox" value="1" <% If U.vCommend = 1 Then Response.Write 'checked="checked"'%> />原创
    </label>
    <label>
      <input name="oTreasure" type="checkbox" value="1" />珍藏
    </label>
    

    然后,你可以使用以下优化后的JavaScript代码来实现在点击勾选框时在输入框中添加内容的功能:

    var checkBox = document.querySelector('input[name="oCommend"]');
    var treasureBox = document.querySelector('input[name="oTreasure"]');
    var inputBox = document.querySelector('input[name="oTitle"]');
    
    function appendContent(content) {
        inputBox.value += content;
    }
    
    function removeContent(content) {
        inputBox.value = inputBox.value.replace(content, '');
    }
    
    checkBox.addEventListener('change', function() {
        if (this.checked) {
            appendContent('【原创】');
        } else {
            removeContent('【原创】');
        }
    });
    
    treasureBox.addEventListener('change', function() {
        if (this.checked) {
            appendContent('【珍藏】');
        } else {
            removeContent('【珍藏】');
        }
    });
    

    这样,当用户点击勾选框时,可以在输入框中添加相应的内容。如果用户取消勾选,相应的内容也会从输入框中删除。

    希望这个解决方案对你有帮助!如果你有任何其他问题,请随时问我。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月20日
  • 已采纳回答 10月12日
  • 创建了问题 10月12日

悬赏问题

  • ¥50 python批量提取发票的信息
  • ¥15 mysql安装,初始化数据库失败
  • ¥15 虚幻五引擎内容如何上传至网盘?
  • ¥15 使用mmpose库时出现了问题
  • ¥15 IRI2016模型matlab运行报错
  • ¥50 bat怎么设置电脑后台自动点击网页指定词运行脚本,输入指定网页链接,指定点击词,指定间隔时间,指定网页出现的词,指定网页出现词出现后后点击锁定,放在后台运行不影响前台鼠标工作
  • ¥20 20CrMnMo的高温变形抗力
  • ¥15 RTX3.6 5565驱动中断报错
  • ¥50 带防重放token(Antireplay-Token)的网站怎么用Python发送请求
  • ¥15 visa版本没问题,串口调试助手调试串口正常使用,但是labview刷新不出来