2016-10-17 98 views
0

我通過組合兩個字段'version'和'productName'來生成字段'端點',但它像空一樣考慮周全,我不知道爲什麼。document.getElementById()。value empty

function generateEndpoint() { 
 
    var yourSelect = document.getElementById("version"); 
 
    document.getElementById('endpoint').value = '/' + 
 
    document.getElementById('productName').value + '/' + 
 
    yourSelect.options[yourSelect.selectedIndex].value; 
 

 
}
<div class="form-group" ng-class="{ 'has-error': invalid.productName, 'has-success': valid.productName}"> 
 
    <label for="productName">product name *</label> 
 
    <input type="text" id="productName" name="productName" class="form-control" placeholder="example: myapi_fr_product " ng-model="api.productName" ng-required="true" onkeyup="generateEnpoint()"> 
 
    <span id="helpBlock" class="help-block" ng-show="help.productName.required">product name is required.</span> 
 
</div> 
 

 
<div class="form-group" ng-class="{ 'has-error': invalid.version, 'has-success': valid.version}"> 
 
    <label for="version">version *</label> 
 
    <select name="version" id="version" class="form-control" ng-init="api.version = api.version || ''" ng-model="api.version" ng-required="true" onchange="generateEndpoint()"> 
 
    <option value=""></option> 
 
    <option value="beta">beta</option> 
 
    <option value="v1">v1</option> 
 
    </select> 
 
    <span id="helpBlock" class="help-block" ng-show="help.version.required">version is required.</span> 
 
</div> 
 

 
<div class="form-group" ng-class="{ 'has-error': invalid.endpoint, 'has-success': valid.endpoint}"> 
 
    <label for="endpoint">exposure endpoint base path *</label> 
 
    <input type="text" id="endpoint" name="endpoint" class="form-control" placeholder="example: /myapi/v1" ng-model="api.endpoint" ng-required="true"> 
 
    <span id="helpBlock" class="help-block" ng-show="help.endpoint.required">exposure endpoint is required.</span> 
 
</div>

我怎樣才能解決我的問題?

非常感謝。

+0

我不能重現該問題。更改選擇可成功讀取第一個輸入的值。更改第一個輸入會遇到完全不同的問題(如果查看錯誤消息,這很明顯)。 – Quentin

+0

如果您選擇下拉列表,則產品名稱+版本的值將添加?。 –

+0

但是,如果使用Angular.JS,代碼將按預期工作,您不應直接訪問DOM。看到這個例子的AngularJS選擇。 – Jaco

回答

0

僅僅通過onkeypress="generateEndpoint()"

function generateEndpoint() { 
 
    var yourSelect = document.getElementById("version"); 
 
    document.getElementById('endpoint').value = '/' + 
 
    document.getElementById('productName').value + '/' + 
 
    yourSelect.options[yourSelect.selectedIndex].value; 
 

 
}
<div class="form-group" ng-class="{ 'has-error': invalid.productName, 'has-success': valid.productName}"> 
 
    <label for="productName">product name *</label> 
 
    <input type="text" id="productName" name="productName" class="form-control" placeholder="example: myapi_fr_product " ng-model="api.productName" ng-required="true" onkeypress="generateEndpoint()"> 
 
    <span id="helpBlock" class="help-block" ng-show="help.productName.required">product name is required.</span> 
 
</div> 
 

 
<div class="form-group" ng-class="{ 'has-error': invalid.version, 'has-success': valid.version}"> 
 
    <label for="version">version *</label> 
 
    <select name="version" id="version" class="form-control" ng-init="api.version = api.version || ''" ng-model="api.version" ng-required="true" onchange="generateEndpoint()"> 
 
    <option value=""></option> 
 
    <option value="beta">beta</option> 
 
    <option value="v1">v1</option> 
 
    </select> 
 
    <span id="helpBlock" class="help-block" ng-show="help.version.required">version is required.</span> 
 
</div> 
 

 
<div class="form-group" ng-class="{ 'has-error': invalid.endpoint, 'has-success': valid.endpoint}"> 
 
    <label for="endpoint">exposure endpoint base path *</label> 
 
    <input type="text" id="endpoint" name="endpoint" class="form-control" placeholder="example: /myapi/v1" ng-model="api.endpoint" ng-required="true"> 
 
    <span id="helpBlock" class="help-block" ng-show="help.endpoint.required">exposure endpoint is required.</span> 
 
</div>

更換onkeyup="generateEndpoint()" ANGULARJS方式

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.api = {}; 
 
    $scope.update = function() { 
 
    $scope.api.endpoint = $scope.api.productName + "/" + $scope.api.version; 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    <div class="form-group" ng-class="{ 'has-error': invalid.productName, 'has-success': valid.productName}"> 
 
    <label for="productName">product name *</label> 
 
    <input type="text" id="productName" name="productName" class="form-control" placeholder="example: myapi_fr_product " ng-model="api.productName" ng-required="true" ng-change="update()"> 
 
    <span id="helpBlock" class="help-block" ng-show="help.productName.required">product name is required.</span> 
 
    </div> 
 

 
    <div class="form-group" ng-class="{ 'has-error': invalid.version, 'has-success': valid.version}"> 
 
    <label for="version">version *</label> 
 
    <select name="version" id="version" class="form-control" ng-init="api.version = api.version || ''" ng-model="api.version" ng-required="true" ng-change="update()" ;> 
 
     <option value=""></option> 
 
     <option value="beta">beta</option> 
 
     <option value="v1">v1</option> 
 
    </select> 
 
    <span id="helpBlock" class="help-block" ng-show="help.version.required">version is required.</span> 
 
    </div> 
 

 
    <div class="form-group" ng-class="{ 'has-error': invalid.endpoint, 'has-success': valid.endpoint}"> 
 
    <label for="endpoint">exposure endpoint base path *</label> 
 
    <input type="text" id="endpoint" name="endpoint" class="form-control" placeholder="example: /myapi/v1" ng-model="api.endpoint" ng-required="true"> 
 
    <span id="helpBlock" class="help-block" ng-show="help.endpoint.required">exposure endpoint is required.</span> 
 
    </div> 
 
</div>

+0

沒有修改..對於信息我在角 – Bourg

+0

此代碼正在工作...你不是+我只是按照使用JS的問題的想法。我將以angularJS方式更新 – Weedoze

+0

非常感謝。祝你有個愉快的日子 – Bourg

0

另外,我看到你正在使用AngularJS並且已經定義了ng-model。如果是這樣,您可以直接調用模型比使用document.getElementById().value

function generateEndpoint() { 
    $scope.api.endpoint = $scope.api.productName + $scope.api.version; 

} 
+0

而我在ng-model的輸入「端點」中調用這個函數? – Bourg

相關問題