无法用javascript替换输入值

时间:2017-01-23 11:28:29

标签: javascript

我试图用Javascript更改输入框的值。 我已经尝试使用.val.setAttribute,但价值没有改变。 我无法使用JQuery。

输入框

<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 = ‘mai@domain.com';'
document.getElementsByTagName('head')[0].appendChild(script2);

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;

我希望它可以帮助你,再见。

答案 1 :(得分:-1)

使用Jquery

有点容易
<script>
 $(document).ready(function(){
$("#btn").click(function(){
    alert('old value '+$("#dd").val());
   ($("#dd").prop('value', 'mai@domain.com'));
   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/>