【发布时间】:2017-06-17 05:30:15
【问题描述】:
显然在 DOM 中更改了值,甚至更改了表单的 ID,认为脚本不再有效,但是当您单击“发布”时,没有任何反应。搜索我尝试使用“prop”而不是“attr”,但它仍然不起作用。我认为这是因为“e.preventDefault()”。但我不知道如何禁用它。这是我的代码。 如您所见,一旦发送请求,输入和“发送”按钮将被停用,当接收到值时,它们会更改表单的 ID、按钮的 ID 和名称,并将其重命名为“发布”。
<script>
jQuery(function($){
var pluginUrl = '<?php echo plugin_dir_url( __FILE__ ); ?>' ;
$('[id^="form-search"]').on('submit', function(e) {
e.preventDefault();
$.ajax({
type : 'POST',
url : 'http://localhost/ladoserver/getapi.php',
data : $(this).serialize(),
cache: false,
beforeSend: function(){
$('input#keyword').prop('disabled', true);
$('button#search').prop('disabled', true);
$('#resuls').html('<img src="'+pluginUrl+'../../assets/img/loading.gif" />');
}
}).done(function(data) {
$('#results').html(data);
$('input#keyword').prop('value', 'PUBLISH DATA');
$('button#search').prop({disabled: false, id:'publish', name:'publish'}).html('Publish');
$('span#help').hide();
$('[id^="form-search"]').prop('action','publish.php');
$('[id^="form-search"]').prop('id', 'form-publish');
var countResults = $('input#total_data').val();
for (var i = 1; i <= countResults; i++) {
$('#lista>select').clone().prop({ id: "cat_"+i, name: "cat_"+i}).appendTo('.category_'+i);
}
$('#lista').remove();
});
});
});
</script>
这是我的html
<form action="" method="post" class="form-search" id="form-search">
<fieldset>
<!-- Form Name -->
<legend>Name</legend>
<div class="form-group">
<input id="keyword" name="keyword" required="" type="text">
<button id="search" name="search" class="btn btn-success boton">Search</button>
<span class="help-block" id="help">Help</span>
<div id="lista" style="display:none">Test</div>
</div>
</fieldset>
<div id="result"></div>
</form>
这里有一个小提琴显示发生的事情(检查 DOM)。 JSFiddle.net
【问题讨论】:
-
您能否发布 html 以及您的小提琴实际上不会从您当前发布的代码的角度阐明您想要实现的目标..
-
好的,我添加了我的html。正如您所看到的,一旦我通过 AJAX 获得结果,我就更改了表单的“动作”以及按钮(名称和 ID),从而成为一个“新”表单。现在当我想给“发布”按钮时,这不起作用,我应该被重定向到 php 脚本页面。
-
ok..现在从您的代码中我可以看到您正在将操作和 id 属性添加到您的表单但是再次提交表单时它的处理程序在哪里我的意思是您应该再添加一个表单发布操作的处理程序
-
我是使用 jQuery 的业余爱好者,我认为更改表单 ID 将被视为新的,激活新的发布按钮将作为普通表单处理。我该如何解决这个问题?
-
好的..我会添加一些东西来帮助..
标签: javascript jquery html forms preventdefault