寻找一些js范围的黑客(Looking for some js scope hack)
这里有一些你可能或可能不知道的javascript函数参数变量的有趣行为:
function foo(bar) { console.log('bar was:', bar); arguments[0] = 'zap'; console.log('bar now:', bar); } foo('bam'); // bar was: bam // bar now: zap
正如您所见,
bar
变量现在指向另一个值。我想以一种稍微奇怪的方式利用这种行为,我想知道是否有可能以某种方式从函数范围外部改变参数值?
也许使用call / apply或其他一些trycky js功能?
所以我可以在调用后更改函数参数的值,例如:
function chooseNumber(number) { setInterval(function() { console.log('I choosed:', number) }, 1000) } chooseNumber(1); // I choosed: 1 // I choosed: 1 // I choosed: 1 // ...
然后,如果我决定改变主意,我怎样才能输出初始函数:
// I choosed: 2 // I choosed: 2 // I choosed: 2 // ...
Here some interesting behaviour of javascript function arguments variable that you may be or maybe not aware of:
function foo(bar) { console.log('bar was:', bar); arguments[0] = 'zap'; console.log('bar now:', bar); } foo('bam'); // bar was: bam // bar now: zap
As you can see
bar
variable now pointing out to another value.I want to make use of such behavior in a slightly strange way, I want to know is it possible to change argument value from the outside of the function scope somehow?
Maybe using call/apply, or some other trycky js features?
So I can change the value of function argument after it was called, for example:
function chooseNumber(number) { setInterval(function() { console.log('I choosed:', number) }, 1000) } chooseNumber(1); // I choosed: 1 // I choosed: 1 // I choosed: 1 // ...
Then if I dicided to change my mind, how can I make so that initial function would output:
// I choosed: 2 // I choosed: 2 // I choosed: 2 // ...
原文:https://stackoverflow.com/questions/18290276
最满意答案
尝试这个:
<%= f.submit :onclick => "return confirm('Are you really sure you want to use '+ document.getElementById('number_of_bars_id').value + '?' )" %>
将
number_of_bars_id
替换为您的字段的ID。如果你稍微谷歌,你会发现一种方法可以使用
:confirm
。Try this:
<%= f.submit :onclick => "return confirm('Are you really sure you want to use '+ document.getElementById('number_of_bars_id').value + '?' )" %>
Replace the
number_of_bars_id
with the id of your field.If you google a bit you will find a way for this to work with
:confirm
.
相关问答
更多-
您可以通过action=file1.php和action=file2.php将表单发送到不同的处理程序。 他们是否使用一堆相同的代码进行处理? 将其放入单独的文件中,包含共同点,并在每个处理文件中写入唯一的位。 不要破解,组织。 对于Javascript验证,不要停止默认操作,然后继续,而是这样做: if (validation != valid) { return false; } 这样,如果JS关闭或验证失败,表单动作/事件是完好的,它的行为如预期的,否则它bonks。 当然,肯定会保留服务器 ...
-
表格提交前确认(Confirm Before Form Submission)[2022-07-17]
来自OP的评论: echo "onclick='return confirm(\'Are you sure you want to submit this form?\');'" 您不能在用单引号分隔的属性值中使用单引号。 你的选择是: 在JS中使用双引号 echo "onclick='return confirm(\"Are you sure you want to submit this form?\");'" 在HTML中使用双引号 echo "onclick=\"return confirm(' ... -
在您的控制器中,您可以访问request对象。 这有一个由referrer名称的属性。 request.referrer将为您提供请求来自的网站。 Im your controller you have access to a request object. This has a attribute by the name of referrer. request.referrer will get you the site the request has come from.
-
这样的事怎么样? 我们的想法是在提交事件上捕获(并触发!),而不是点击事件。 $(document).ready(function () { $('#myForm').submit(function() { var f = this; confirm("Really delete this event?", function () { $(f).submit(); }); return false; // ca ...
-
对不起,你必须使用JS(确认也是Rails提供的JS功能)。 如果您需要有关JS实现的更多帮助,您应该提供更多信息(和代码)。 I'm sorry, but you have to use JS (the confirmation is also a JS function provided by Rails). You should give more info (and code) if you need more help about the JS implementation.
-
你可以添加changed? 检查你的模板: <% if @post.changed? && @post.invalid? %><% @post.errors.full_messages.each do |message| %> <%= message %><% end %> You may add changed? check to your template ...
<% end %> -
表单多次提交(Form submitted multiple times)[2022-10-09]
每次载入主要内容时,似乎都会载入JavaScript。 因此每次更改事件都与表单绑定。每次加载主内容时都需要删除以前的绑定。 在$('body').on('submit', 'form', function(e){ $(body).off("submit", "form"); 它会关闭以前的绑定并保持一个绑定存活。 Each time you load the main content it seems you javascript also loading with it. So change even ... -
当我将代码替换为现有simple_form代码上的按钮时,它似乎有效。 嗯你能发布simple_form的代码吗? So it turns out this was a problem with the jquery-rails gem. https://github.com/rails/jquery-rails/issues/173 In my gem file, I changed from this: gem 'jquery-rails' To this: gem 'jquery-rails', '~ ...
-
Rails的Ajax表单提交消息(Rails ajax form submit message)[2022-10-23]
你快到了。 提交的表单由创建操作处理,而不是由新操作处理。 你有正确处理html或js的语法 - 你只需要在你的创建动作中使用它,以及一个包含你要渲染的js的create.js.erb。 你的PostsController应该有一个创建函数 def create @post = Post.new(params[:post]) respond_to do |format| if @post.save format.html format.js else ... -
尝试这个: <%= f.submit :onclick => "return confirm('Are you really sure you want to use '+ document.getElementById('number_of_bars_id').value + '?' )" %> 将number_of_bars_id替换为您的字段的ID。 如果你稍微谷歌,你会发现一种方法可以使用:confirm 。 Try this: <%= f.submit :onclick => ...