[英]How to fill web form using javascript
I want to fill the form data using javascript in chrome. 我想在Chrome中使用javascript填写表单数据。 I have filled all other fields correctly but the field(html) bellow is not working for me.
我已经正确填写了所有其他字段,但是field(html)波纹管不适用于我。 (there must be other scripts working this form, i don't about that)
(必须有其他脚本在使用此表单,我对此不满意)
<select name="month" class="valid">
<option value="">
<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Month</font>
</font>
</option>
<option value="01">
<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">January</font>
</font>
</option>
<option value="02">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">February</font>
</font>
</option>
<option value="03">
<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">March</font>
</font>
</option>
Month January February March 月一月二月三月
i have tried some code but it doesn't work. 我已经尝试了一些代码,但是没有用。
var month = document.querySelector('input[name="month"]');
month.selectedIndex = 3;
i am using this script in chrome console, is there any way i can fill this field with javascript? 我在chrome控制台中使用此脚本,有什么办法可以用javascript填充此字段?
First, your selector input[name="month"]
does not match any elements. 首先,您的选择器
input[name="month"]
与任何元素都不匹配。
Secondly, once you are able to select the correct element you can set it's value using element.value = 3
. 其次,一旦您能够选择正确的元素,就可以使用
element.value = 3
设置其值。
See more about the value property here. 在此处查看有关value属性的更多信息。
This code does exactly what you looking for. 这段代码正是您想要的。 It selects March in your select.
它会选择三月。 Note: correct selection in not var month = document.querySelector('input[name="month"]');
注意:在不是var month = document.querySelector('input [name =“ month”]');中的正确选择 Use instead
改用
var month = document.querySelector('select[name="month"]');
$(function() { var month = document.querySelector('select[name="month"]'); month.selectedIndex = 3; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="month" class="valid"> <option value=""> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Month</font> </font> </option> <option value="01"> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">January</font> </font> </option> <option value="02"> <font style="vertical-align: inherit;"> <font style="vertical-align: inherit;">February</font> </font> </option> <option value="03"> <font style="vertical-align: inherit;"><font style="vertical-align: inherit;">March</font> </font> </option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.