2017-08-24 70 views
0

我想通過javascript自動填充表單。現在,我遇到的問題是,在填寫完這些文本框後,我將其重點放在了另一個文本框中,我填寫的所有文本都已清除並清空了文本框?有人能告訴我爲什麼這樣做,以及我如何通過它。JS自動填寫表單,文本框清除焦點?

JS:

window.setTimeout(function() { 
    document.getElementsByClassName("_ph6vk _623pz _o716c")[0].focus(); 
}, 1000); 

window.setTimeout(function() { 
    document.getElementsByClassName("_ph6vk _623pz _o716c")[0].value = "some value 0"; 
}, 2000); 

window.setTimeout(function() { 
    document.getElementsByClassName("_ph6vk _623pz _o716c")[1].focus(); 
}, 3000); 

window.setTimeout(function() { 
    document.getElementsByClassName("_ph6vk _623pz _o716c")[1].value = "some value 2"; 
}, 4000); 

HTML:

<form class="_asaim"> 
    <div class="_fhskl"> 
     <div class="_j5suk"></div> 
     <div class="_hxmdu"> 
      or 
     </div> 
     <div class="_j5suk"></div> 
    </div> 
    <div class="_agqzm"> 
     <div class="_sjplo _8ki4j"> 
      <input aria-describedby="" aria-label="Mobile Number or Email" aria-required="true" class="_ph6vk _623pz _o716c" name="emailOrPhone" placeholder="Mobile Number or Email" type="text" value="d"> 
      <div class="_gaby6"> 
       <span class="coreSpriteInputError _eyp7r"></span> 
      </div> 
     </div> 
    </div> 
    <div class="_agqzm"> 
     <div class="_sjplo _8ki4j"> 
      <input aria-describedby="" aria-label="Full Name" aria-required="false" class="_ph6vk _623pz _o716c" name="fullName" placeholder="Full Name" type="text" value="d"> 
      <div class="_gaby6"> 
       <span class="coreSpriteInputAccepted _eyp7r"></span> 
      </div> 
     </div> 
    </div> 
    <div class="_agqzm"> 
     <div class="_sjplo _8ki4j"> 
      <input aria-describedby="" aria-label="Username" aria-required="true" class="_ph6vk _623pz _o716c" maxlength="30" name="username" placeholder="Username" type="text" value="d"> 
      <div class="_gaby6"> 
       <span class="coreSpriteInputError _eyp7r"></span> 
      </div> 
     </div> 
    </div> 
    <div class="_agqzm"> 
     <div class="_sjplo _8ki4j"> 
      <input aria-describedby="" aria-label="Password" aria-required="true" class="_ph6vk _623pz _o716c" name="password" placeholder="Password" type="password" value="d"> 
      <div class="_gaby6"> 
       <span class="coreSpriteInputError _eyp7r"></span> 
      </div> 
     </div> 
    </div> 
    <div> 
     <span class="_9mno0 _ov9ai"><button class="_qv64e _gexxb _4tgw8 _njrw0"><span class="_9mno0 _ov9ai">Sign up</span></button></span> 
    </div> 
    <p class="_fd2m9"><!-- react-text: 144 -->By signing up, you agree to our <!-- /react-text --><a class="_nzfks" href="/legal/terms/" target="_blank">Terms</a><!-- react-text: 146 --> &amp; <!-- /react-text --><a class="_nzfks" href="/legal/privacy/" target="_blank">Privacy Policy</a><!-- react-text: 148 -->.<!-- /react-text --></p> 
</form> 

回答

0

這很難說沒有看到所有的代碼,但是你已經發布的代碼僅僅是在你輸入後,在一定的設定值時間已過(你在setTimeout函數中指定的毫秒數),所以我懷疑你正在做一些事情導致這些超時函數被調用,這會覆蓋輸入。我猜測輸入的原因是已清除可能是例如值「某個值0」實際上並不是作爲相關輸入的「值」存在的,所以輸入只是被清除,因爲找不到指定的值?除非您發佈完整的代碼,否則我無法完全確定。