我正在尝试为搜索框创建弹出窗口并提交关键字 用一个表格。我在互联网上浏览类似的问题,但我应用的唯一解决方案是 删除表单的提交元素。也没有成功;有什么想法吗?
在 django 上工作,我可以提供更多细节。
我的 JavaScript 的某些部分如下所示:
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 200,
width: 200,
modal: true,
buttons: {
"Search": function() {
var bValid = true;
allFields.removeClass( "ui-state-error" );
bValid = bValid && checkLength( keyword, "username", 2, 16 );
bValid = bValid && checkRegexp( keyword, /^[a-z]([0-9a-z_])+$/i, "Keyword may consist of a-z, 0-9, underscores, begin with a letter." );
if ( bValid ) {
document.getElementById('searchForm_1_keyword').value = keyword.val();
document.getElementById('searchForm').submit(); //if I comment out this line it continues the next line, if not, it does not execute the next line
$( "#testDiv" ).append( "<tr>" +
"<td>" + keyword.val() + "</td>" +
"</tr>" );
alert(keyword.val());
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
html 看起来像这样:
<div class="searchBox">
<form id='searchForm' method="POST" action="/dasdasasdas/" >{% csrf_token %}
<input type="text" name="column_name" value='supplier_name'>
<input type="text" name="keyword" id="searchForm_1_keyword" value=""/>
</form>
</div>
这些是我的 jQuery 文件:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>
找到解决方案!
我犯了一个新手错误。我有嵌套表格。当我纠正那个错误时,它确实起作用了。
最佳答案
你为什么要用
document.getElementById('searchForm').submit()
什么时候可以使用 jQuery?你可以这样做:
$("#searchForm").submit()
关于javascript - 无法从 JavaScript 触发提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9994874/