【发布时间】:2020-12-25 22:06:02
【问题描述】:
我的 JQuery 函数应该修改隐藏表单字段的值,然后提交表单。但它不会。小提琴here.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(function(){
// when clicking the submit button
$('.findme').click(function(){
// Get the value of the clicked a tag
var index = $(this).attr("id");
// Strip out the prefix, leaving only the id
var usethisID = index.substring(index.indexOf("_")+1 );
alert('Value to post: ' + usethisID) ;
// Make a hidden form field and set its value to usethisID
var x = document.createElement("INPUT");
x.setAttribute("type", "hidden");
x.setAttribute("name", "xid");
x.setAttribute("id", "xid");
x.setAttribute("value", usethisID);
// submit the form
$('#fruitform').submit();
});
});
</script>
【问题讨论】:
-
显示您的表格 m8。
-
还有 - 你是不是缺少
appendChild或类似的东西 - 你把你的X放在哪里? -
喜欢
$('#fruitform').append(x).submit();? -
是的,我可以看到 createElement 并不是专门在fruitform中创建隐藏字段。但不管怎样,它只是不提交。表单代码在phpfiddle.org/main/code/5g1s-s8vz
标签: jquery forms post submit hidden