Change `<input>` element attributes from obj

2019-09-19 19:42发布

问题:

I'm trying to change the value of an element in a <input type="checkbox"> from null to checked.
How would I do this from objective-C (iOS application)?

Here's the relevant HTML of the page

<td align="right" width="15%" class="darkNeutral">
    <input id="chk_Assignments" type="checkbox" class="ShowAll" />
    <label for="chk_Assignments">Show All</label>

    &nbsp;&nbsp;
</td>

I was thinking I'd use POST or something, but I'm not sure how to access the element, add a checked attribute, and set the value of the attribute to "checked".

回答1:

NSString *javaScript = @"document.getElementById('chk_Assignments').setAttribute('checked', 'checked');";

// Make the UIWebView method call
NSString *response = [webViewA stringByEvaluatingJavaScriptFromString:javaScript];
NSLog(@"javascript result: %@", response);

If HTML page got jQuery included, the javascript could be simplified:

NSString *javaScript = @"$('#chk_Assignments').attr('checked', 'checked');";
NSString *response = [webViewA stringByEvaluatingJavaScriptFromString:javaScript];
NSLog(@"javascript result: %@", response);