简体   繁体   English

通过jQuery更改输入后提交表单

[英]Submitting form after changing input by jQuery

I got HTML like this: 我有这样的HTML:

<table>
    <form action=\'food.php\' method=\'POST\'>
    <tr><h4>
        <td><span><input type="submit" name="food_edit" class="food_edit" value="Edytuj" /></span></td>
       </h4>
    </tr>
    </form>
</table>

And then a function in jQuery which change input field: 然后是jQuery中的一个函数,它更改输入字段:

wrap.find('.food_edit')
    .replaceWith('<input type=\'submit\' name=\'food_edit_confirm\' value=\'Potwierdź\' />');

My problem is that after change submitting button, send form doesn't work. 我的问题是更改提交按钮后,发送表单不起作用。

you may try this after replacement of submit button: 您可以在替换“提交”按钮后尝试以下操作:

$('form').on('click','input[type=submit]',function(){
   //rest of code goes here.
});

you either need to use $( document ).ready() around the particular jquery code or u can try and use the .attr to change the attribs of your class or u can use 'focus' to wrap around it. 您可以在特定的jquery代码周围使用$(document).ready(),或者您可以尝试使用.attr来更改类的属性,或者您可以使用'focus'对其进行环绕。 or u can do something like 或者你可以做类似的事情

$('form').live('submit',function(){
// do the rest of ur code and then submit it....
});

You have invalid HTML. 您的HTML无效。 It should look like this: 它看起来应该像这样:

<form action=\'food.php\' method=\'POST\'>
    <table>
        <tr>
            <td>
                <span>
                    <input type="submit" name="food_edit" class="food_edit" value="Edytuj" />
                </span>
            </td>
        </tr>
    </table>
</form>

If you're looking to add a confirmation you should look into the onsubmit="" in the <form> tag. 如果要添加确认,则应查看<form>标记中的onsubmit=""

Example: 例:

<form action=\'food.php\' method=\'POST\' onsubmit=\'return checkForm();\'>
    <table>
        <tr>
            <td>
                <span>
                    <input type="submit" name="food_edit" class="food_edit" value="Edytuj" />
                </span>
            </td>
        </tr>
    </table>
</form>

JS Code: JS代码:

function checkForm() {
    return confirm("Submit this form?");
}
<script type="text/javascript">
$(function(){
$('.food_edit').replaceWith("<input type='submit' name='food_edit_confirm' value='Potwierdz' onclick='document.getElementById(\"submit\").click();' />");

});
</script>


<form action='food.php' method='POST'>
        <input type="submit" name="food_edit" class="food_edit" value="Edytuj" onclick="document.getElementById('submit').click();" />
        <input type="submit" style="display:none" id="submit" />
    </form>

The hidden button always do submit the form regardless you do anything with the submit button which is replacable. 无论您使用可替换的提交按钮执行任何操作,隐藏按钮始终会提交表单。

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

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