首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将必填字段设置为隐藏/显示切换

将必填字段设置为隐藏/显示切换
EN

Stack Overflow用户
提问于 2018-06-07 07:56:49
回答 1查看 51关注 0票数 0

我有一个切换功能,可以隐藏例如输入文本或不隐藏。我想在没有隐藏的情况下设置一个required (但如果它被隐藏,它不应该是required警报。

我尝试将[required]window.alert设置为id,例如测试(见下文),但不起作用。有什么建议可以解决这个问题吗?

非常感谢您的支持和反馈

代码语言:javascript
复制
<tr>
    <td>Ice: </td>
    <td><input id="test" name="test" type="text" size="40">
    <button class="test btn" type="button" onClick="$('#test').toggle();">Hide</button></td>
</tr>   

$(".test").click(function() { 
   var lable = $(".test").text().trim();

   if(lable == "Hide") {
     $(".test[required]").text("Show").css( "background", "yellow").css ("color", "black").css("padding","1px 12px")
     return window.alert('please fill out');
     $(".text").hide().css( "background", "orange").css ("color", "black").css("padding","1px 12px");
   }
   else {
     $(".test").text("Hide").css( "background", "orange").css ("color", "black").css("padding","1px 12px");
     $(".text[required]").show().css( "background", "yellow").css ("color", "black").css("padding","1px 12px")
     return window.alert('please fill out');
       }

  });   
EN

回答 1

Stack Overflow用户

发布于 2018-06-07 08:16:19

您可以执行以下操作将其设置为required:

代码语言:javascript
复制
$('.test').attr('required', '');

要删除所需的内容,您可以执行以下操作:

代码语言:javascript
复制
$('.test').removeAttr('required');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50731221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档