I want to update the selected value of checkbox for javascript function in iOS. This is the HTML code for checkbox:
<input type='checkbox' name='displayraw' value='yes' selected='true' >
I want to change the selected= "false" or "true"
when checkbox is checked or unchecked.
And here is my code: I used the webview to run submit function but cannot successful. I can change the value for textbox but cannot change the selected value for check box. Please help me. Thanks in advance.
NSString* temp = [self.webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:
@"document.getElementsByName('%@')[0].selected='%@'",elementName, value]];
Checkboxes in HTML are checked
NOT selected
.
So if you simply change your code to something like this:
(notice how I have changed "selected" to "checked" in both examples):
Example HTML:
<input type='checkbox' name='displayraw' value='yes' checked='checked' />
Example Objective-C:
NSString* temp = [self.webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:
@"document.getElementsByName('%@')[0].checked='%@'",elementName, value]];
So if you make sure that your javascript string in Objective-C is being evaluated like this:
// use this
document.getElementsByName('displayraw')[0].checked = true;
Then it should check your checkbox.
UPDATE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To uncheck an 'checked' checkbox use:
document.getElementsByName('displayraw')[0].checked = false;
To check an 'unchecked' checkbox, use:
document.getElementsByName('displayraw')[0].checked = true;
SEE THIS JAVASCRIPT IN ACTION VIA JSFIDDLE HERE
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.