简体   繁体   English

仅在输入字段不为空且具有有效URL时显示按钮

[英]only show button if input fields are not empty and one has valid URL

I have the following code and it works fine to check if the input field "inputURL" has a valid URL, if it has the anchor link a.btn shows fine but I also only want it to show if both have been filled in. How can I add to the ng-show in the anchor to do this? 我有以下代码,并且可以很好地检查输入字段“ inputURL”是否具有有效的URL,如果它具有锚链接a.btn显示,但是我也只希望它们都被填充。我可以在锚中添加ng-show来做到这一点吗?

<form name="myForm" class="row inputs">
          <div class="col-xs-5">
              <label for="exampleInputPassword1">Enter a Title/Description</label>
                <input type="text" id="urlName" class="form-control" placeholder=""  ng-model="mvName" >
            </div>
            <div class="col-xs-5">
                <label for="exampleInputPassword1">Enter a URL</label>
                <input type="url" name="inputURL" id="urlLink" class="form-control" placeholder=""  ng-model="mvUrl" >
          </div>
          <div class="col-xs-2">
              <a href="javascript:" ng-click="saveToList($event)" class="btn btn-block post" ng-show="myForm.inputURL.$valid  ">Post</a>
          </div>
        </form>

You can check if both have been filled with the model like this: 您可以检查两个模型是否都已填充,如下所示:

ng-show="myForm.inputURL.$valid && mvName && nvUrl"

or with the view value of the input 或输入的视图值

ng-show="myForm.inputURL.$valid && myForm.inputURL.$viewValue && myForm.urlName.$viewValue"

You can use the required HTML5 special attribute to check if the input is not empy. 您可以使用必需的HTML5特殊属性来检查输入是否不是empy。

 <form name="myForm" class="row inputs">
      <div class="col-xs-5">
          <label for="exampleInputPassword1">Enter a Title/Description</label>
            <input type="text" name="mvName id="urlName" class="form-control" placeholder=""  ng-model="mvName" required>
        </div>
        <div class="col-xs-5">
            <label for="exampleInputPassword1">Enter a URL</label>
            <input type="url" name="inputURL" id="urlLink" class="form-control" placeholder=""  ng-model="mvUrl" required>
      </div>
      <div class="col-xs-2">
          <a href="javascript:" ng-click="saveToList($event)" class="btn btn-block post" ng-show="myForm.inputURL.$valid && !myForm.inputURL.$error.required && !myForm.mvName.$error.required">Post</a>
      </div>
    </form>

However, to clean the view a little bit, I would suggest using a controller. 但是,为了稍微清理一下视图,我建议使用控制器。 Expose a function which returns true or false if the form is valid. 公开一个函数,如果该表单有效,则该函数返回true或false。 And simply use ng-show="isValid() ". 只需使用ng-show =“ isValid() ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当输入有效时才显示按钮 - Show Button only if Input is Valid 如果以前的输入具有有效值,我该如何显示输入字段? - How can I show input fields only when previous input has a valid value? 仅在所有输入字段为空时显示错误消息框 - Show error message box only if all input fields are empty 如果输入不为空,则显示按钮 - Show button if input is not empty 如何在每个步骤中验证必填字段? (也就是说,只有在必填字段有效且不为空时,下一个按钮才起作用) - How to validate in each step the required fields? (that is, the next button should only work if required fields are valid and not empty) 仅在空白字段中显示错误 - Show error only in empty fields jQuery:仅当一个输入具有值时,才需要所有字段 - jQuery: Make all fields required only if one input has a value 在尝试隐藏提交按钮直到所有输入字段均有效之前,它是否仅对第一个文本字段验证隐藏? - While trying to hide submit button until all input fields are valid it is hiding only for the first text filed validation? 在输入字段为空时显示警报并在单击提交按钮时显示错误消息 - to dispaly alerts whenever the input fields are empty and show the eror message while clicking submit button 在空白的输入字段中显示占位符文本 - Show placeholder text in empty input fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM