简体   繁体   English

提交时Jquery prop值更改将不起作用

[英]Jquery prop value change on submit won't work

$( function() {
    $( "#save-chart-form" ).submit( function( event ) {
        $("#saveChartButton").prop( 'value', 'Please be patient' );
        // event.preventDefault() // works with this
        // $('#save-chart-form').submit(); // Doesn't work
    } );
} );

I run this on a form #save-chart-form when #saveChartButton is clicked. 当单击#saveChartButton时,我在#save-chart-form表单上运行它。 So the text of the button is changed to 'Please be patient' when button is clicked. 因此,单击按钮时,按钮的文本将更改为“请耐心等待”。

This doesn't work at all unless I put a preventDefault event. 除非我发出了preventDefault事件,否则这根本不起作用。 Is there no way to have any jQuery action performed on form submission? 是否有办法在表单提交上执行任何jQuery操作? I tried many other solutions that all fail. 我尝试了许多其他解决方案都失败了。 Also I can't get $('#save-chart-form').submit() do anything, which would be a solution. 此外,我无法获得$('#save-chart-form').submit()做任何事情,这将是一个解决方案。 I'm using jQuery 3.1.1 我正在使用jQuery 3.1.1

EDIT: It's a browser issue: it works correctly on Firefox. 编辑:这是一个浏览器问题:它在Firefox上正常工作。 Safari doesn't process anything after unload request as mentioned by Rory McCrossan. 如Rory McCrossan所述,Safari在卸载请求后不会处理任何内容。

don't use $('#save-chart-form').submit() . 不要使用$('#save-chart-form').submit() It's redundant. 这是多余的。

$(function(){
    $("#save-chart-form" ).submit(function(event){
        $("#saveChartButton").prop('value', 'Please be patient');
    });
});

The issue is because in most browsers the UI will no longer be updated once a request to unload the page is made (ie. the form submission has been made). 问题是因为在大多数浏览器中,一旦发出卸载页面的请求(即已经提交表单),UI将不再更新。 A workaround would be to change the button text under the click event of the button itself, instead of the submit event of the form . 解决方法是更改​​按钮本身的click事件下的按钮文本,而不是formsubmit事件。 Try this: 尝试这个:

$("#saveChartButton").click(function() {
    $(this).prop('value', 'Please be patient');
});

Working example 工作实例

Note that the change is only visible for a small time before the page is unloaded. 请注意,更改仅在卸载页面之前的一小段时间内可见。

Try to show the text "please be patient" in other HTML element and not the #saveChartButton , because changin the value of a form element that is already submitted can be confusing to the js engine. 尝试在其他HTML元素中显示“请耐心等待”文本,而不是#saveChartButton,因为已经提交的表单元素的值可能会让js引擎混淆。

eg 例如

$("#save-chart-form").submit(function(event) {
    $("#msgBox").html('Please be patient');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM