简体   繁体   English

stringByEvaluatingJavaScript对象没有ID

[英]stringByEvaluatingJavaScript object has no ID

I am using self.webView.stringByEvaluatingJavaScript(from: "document.getElementById('username').value = \\"\\(username)\\"") to put in a username, and it works fine. 我使用self.webView.stringByEvaluatingJavaScript(from: "document.getElementById('username').value = \\"\\(username)\\"")来输入用户名,它工作正常。

A different webpage has this field: 另一个网页有此字段:

<input type="text" maxlength="255" name="user" class="stdWidth" value="">

and this field: 和这个领域:

<input type="password" maxlength="255" password="1" name="pass" class="stdWidth">

How would i use stringByEvaluatingJavaScript to put text into this field, as it has no ID? 我如何使用stringByEvaluatingJavaScript将文本放入此字段,因为它没有ID?

I hace tried "document.getElementByTagName" but it didnt work. 我试过"document.getElementByTagName"但它没有用。

The webpage I am trying to input to is: Link 我想输入的网页是: 链接

Try like this.you have class field so use document.querySelector() to select your element.Example below. 尝试这样。你有class字段,所以使用document.querySelector()来选择你的元素。例如下面。

 var input = document.querySelector(".stdWidth"); input.value = "\\"Hello\\""; //add event listener on edit like this.. input.addEventListener('change',function(){ var value = this.value; console.log(value);//this is current value of your field }); 
 <!-- begin snippet: js hide: false console: true babel: false --> 

UPDATE UPDATE

The getElementsByTagName() method returns a collection of all elements in the document with the specified tag name, as a NodeList object. getElementsByTagName()方法返回文档中具有指定标记名称的所有元素的集合,作为NodeList对象。

 var input = document.getElementsByTagName('input'); input[0].value = "\\"username\\""; input[1].value = "\\"password\\""; 
 <input type="text" maxlength="255" name="user" class="stdWidth" value=""> <input type="text" maxlength="255" name="pass" class="stdWidth" value=""> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM