我有一个切换功能,可以隐藏或不隐藏输入文本。
我想在不隐藏的情况下设置必填项(但如果被隐藏,则不应设置为必需警报。

我试图将[required]window.alert设置为id测试(见下文),但无法正常工作。有什么建议如何解决这个问题?

非常感谢您的支持和反馈

<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');
       }

  });

最佳答案

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

$('.test').attr('required', '');


并删除所需的,您可以执行以下操作:

$('.test').removeAttr('required');

关于javascript - 将必填字段设置为“隐藏/显示”切换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50731221/

10-12 00:16
查看更多