简体   繁体   English

输入类型=提交时更改输入值

[英]Change value of input when input type=submit

I have an input like the one below: 我有一个类似下面的输入:

    <input class="submit" type="submit" name="submit" value="Blah">

I want to change the value to 'Other Text'. 我想将值更改为“其他文本”。 When I use the .val(),attr() or .prop() jQuery method the value changes fine but when I submit, the input doesn't work any more.Just refreshes the page without doing what its supposed to. 当我使用.val(),attr()或.prop()jQuery方法时,该值会很好地改变,但是当我提交时,输入将不再起作用。只需刷新页面而不执行其应有的工作即可。

Same result with .setAttribute method. 与.setAttribute方法相同的结果。

How do I get the input to work correctly now the value has been changed? 值更改后,如何使输入正常工作?

Try this 尝试这个

<input type=button name="b1" value="Submit" onclick="javascript:document.MyForm.b1.value='Done'">

Source : Change text on submit button after submission 来源: 提交后在“提交”按钮上更改文本

Check this 检查一下

 $(document).ready(function(){ $(".submit").click(function(){ $(this).val("OtherText"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="submit" type="submit" name="submit" value="Blah"> 

You can prevent submitting form or in click event of button: 您可以阻止提交表单或在按钮单击事件中:

$(".submit").click(function(e){
    e.preventDefault();
    $("#inputbox").val("OtherText");
    //Now you can submit form manually here
    $("#form").submit();
});

I believe it was server side script was checking the value, so had to change the value back when clicked. 我认为是服务器端脚本正在检查该值,因此单击时必须将其更改回原来的值。

if ($('.submit').length > 0) {
    $('.submit').prop('value', 'Other Text');
    $('.submit').click(function(){
    $('.submit').prop('value', 'blah'); 
    });}

This seems to be working 'ok' now. 现在看来这工作正常。 Not ideal but works 不理想但是可以

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

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