I have a form consisting of only one <input>
element:
<form>
<input
type="text"
name="webpage-activity-url"
placeholder="URL"
value="[value inserted by templating engine]"
/>
<button type="submit" class="button button-secondary" data-action="urlSubmit">
Submit
</button>
</form>
And I also have some javascript (in the form of coffeescript) which is called when the form is submitted:
onURLSubmit: (e)->
e.preventDefault()
# console.log('urlSubmit')
url = @$el.find('input[name="webpage-activity-url"]').val()
if _.trim(url) == ''
return false
if url.indexOf('//') == -1
url = 'http://' + url
unless @$el.find('iframe').length > 0
iframe = $('<iframe />').insertAfter(@$el.find('.instructor-ui-box').first())
@setIframeSrc url
@$el.find('input[name="webpage-activity-url"]').val('')
Here is the same function compiled to javascript, in case you are not familiar with coffeescript:
WebpageActivityLayout.prototype.onURLSubmit = function(e) {
var iframe, url;
e.preventDefault();
url = this.$el.find('input[name="webpage-activity-url"]').val();
if (_.trim(url) === '') {
return false;
}
if (url.indexOf('//') === -1) {
url = 'http://' + url;
}
if (!(this.$el.find('iframe').length > 0)) {
iframe = $('<iframe />').insertAfter(this.$el.find('.instructor-ui-box').first());
}
this.setIframeSrc(url);
return this.$el.find('input[name="webpage-activity-url"]').val('');
};
Here is my problem: when the form is submitted, the value that the user enters into the <input>
element of the form is cleared and returned to the value specified by the placeholder
attribute of the input element. I would like to prevent this behavior. I tried stepping through the javascript function, and I noticed that the value of the input element is reset when the onURLSubmit
function returns . So I don't really know how to prevent this through javascript.
This line...
@$el.find('input[name="webpage-activity-url"]').val('')
...is setting the value to ''
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.