[英]Execute js code before committing “submit” in ruby on rails
i can't seem to make the old js scripts that i found on the magical SO. 我似乎无法制作我在神奇的SO上找到的旧js脚本。
<div class="form-actions">
<%= f.button :submit, class:"btn btn-success create-campaign" %>
</div>
this is my submit button.But there is a problem. 这是我的提交按钮。但是有一个问题。
<div class="message-div" id="textField" contenteditable="true" maxlength="180"></div>
<%= f.input :message ,as: :text,as: :hidden ,input_html: { class: 'special', maxlength:'180' } %>
i use div to get the input for what i have done for it but my form takes its input from the hidden text area. 我使用div来获取我为它所做的事情的输入,但我的表单从隐藏的文本区域获取其输入。
So i tried to do take take the content of the div and write it to the text area when submit button clicked. 所以我尝试采取div的内容,并在单击提交按钮时将其写入文本区域。
but the js script that i wrote does not getting executed. 但我写的js脚本没有被执行。
$(document).on("click",".create-campaign",function(e){
console.log('asdasd');
var a = $('.message-div').html();
document.getElementById('.special').value = a;
});
this is the code. 这是代码。
When i changed the listener class to another button, it works without a problem ( at least i see console log) but not when the submit button is pressed. 当我将监听器类更改为另一个按钮时,它可以正常工作(至少我看到控制台日志)但不按下提交按钮时。
What should i do? 我该怎么办?
Cheers. 干杯。
You need to prevent the default submit action from being triggered: 您需要阻止触发默认提交操作:
$(".create-campaign").click(function(e) {
e.preventDefault();
console.info('asdasd');
var a = $('.message-div').html();
alert(a); // just to check that your data is there
document.getElementById('.special').value = a;
$("#YOUR_FORM_ID").submit();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.