2017-01-23 73 views
0

我在嘗試使用Javascript更改輸入框的值。 我已經嘗試使用.val.setAttribute,但值並未改變。 我無法使用JQuery。無法用javascript替換輸入值

輸入框

<div class="row"><!-- ko if: error --><!-- /ko --> <div class="form-group col-md-24"><!-- ko if: prefillNames().length > 1 --><!-- /ko --><!-- ko ifnot: prefillNames().length > 1 --> <div class="placeholderContainer" data-bind="component: { name: 'placeholder-textbox', params: { 
       serverData: svr, 
       textInput: displayName, 
       hasFocus: isFocused, 
       hintText: str['CT_PWD_STR_Email_Example'], 
       hintCss: 'placeholder' + (!svr.A ? ' ltr_override' : '') } }"><!-- ko withProperties: { '$placeholderText': placeholderText } --> <!-- ko template: { nodes: $componentTemplateNodes, data: $parent } --> <input type="email" name="loginfmt" id="i0116" maxlength="113" lang="en" class="form-control ltr_override" aria-describedby="usernameError" data-bind="textInput: displayName, 
         hasFocusEx: isFocused, 
         placeholder: $placeholderText, 
         ariaLabel: str['CT_PWD_STR_Email_Example'], 
         css: { 'has-error': error }, 
         attr: inputAttributes" aria-label="Test"> 
<input name="passwd" type="password" id="i0118" autocomplete="off" data-bind="moveOffScreen, textInput: passwordBrowserPrefill" class="moveOffScreen" tabindex="-1" aria-hidden="true"><!-- ko if: true --> <div id="usernameProgress" class="progress" role="progressbar" data-bind="visible: isRequestPending" style="display: none;"><div></div><div></div><div></div><div></div><div></div><div></div></div><!-- /ko --> <!-- /ko --><!-- /ko --><!-- ko ifnot: forcePlaceholderAttribute --> <div class="phholder" style="left: 0px; top: 0px; width: 100%; position: absolute; z-index: 5;" data-bind="visible: !textInput(), click: focus"> <div aria-hidden="true" style="cursor:text" data-bind="text: hintText, css: hintCss" class="placeholder">Test</div> </div> <!-- /ko --></div><!-- /ko --> </div> </div> 

這裏是我的代碼:

var script2 = document.createElement(‘script’); 
script2.type = ‘text/javascript’; 
script2.text = ‘document.getElementById('i0116').value = ‘[email protected]';' 
document.getElementsByTagName('head')[0].appendChild(script2); 

感謝您的時間。

+1

這些有角度的引號看起來有點不禮貌,請嘗試使用正常的單引號(')。爲什麼這不是一個普通的腳本塊。當你已經明確擁有一個現有的腳本時,爲什麼要注入一個新的腳本塊?似乎有點毫無意義。運行該代碼時是否遇到任何控制檯錯誤? – ADyson

+0

你爲什麼要將一個新的腳本標記寫入頭部,以便直接執行某些操作?所以:把你代碼片段中的所有代碼替換爲:'document.getElementById(「i0116」)。value =「[email protected]」;' – Pineda

+0

就像@ADyson指出的那樣,小心你的混合**引號**('),** _ double_ quotes **(「)和** back-ticks **(') – Pineda

回答

-1

這是一個有點容易使用jQuery

<script> 
$(document).ready(function(){ 
$("#btn").click(function(){ 
    alert('old value '+$("#dd").val()); 
    ($("#dd").prop('value', '[email protected]')); 
    alert('new value '+$("#dd").val()); 
}); 


}); 
</script> 
</head> 
<body> 

<button id="btn">submit</button> 
<input type='text' id="dd" class='funfoo' placeholder='Fill me'/> <br/><br/> 
+0

對於JavaScript版本 –

+2

,您仍然可以使用document.get ..「我無法使用JQuery」的哪一部分。在問題中不清楚?:-) – ADyson

0

你可以嘗試像以下:

<!DOCTYPE html> 
 
<html> 
 
<body> 
 
    <form> 
 
    <button id="btn" onclick="javascript:test('pwd1')">Set pwd</button> 
 
    <input type='password' id="pwd1"/> <br/><br/> 
 
    </form> 
 
    </body> 
 
    <script> 
 
    function test(id){ 
 
     document.forms[0][id].value='test'; 
 
     alert("Value of " + id + ": " + document.forms[0][id].value); 
 
    } 
 
    </script> 
 
</html>

我希望它可以幫助你,再見。