gpt4 book ai didi

php - 清除输入或搜索按钮上的输入文本字段?

转载 作者:行者123 更新时间:2023-11-30 10:50:42 26 4
gpt4 key购买 nike

我有一个表单可以让我的用户进行搜索。我正在使用 jQuery 将搜索结果附加到页面的较低位置,而无需重新加载页面。所以,我得到了这个:

isbn.php:
<form method="post" action="ajax.php" name="searchISBN" id="searchForm">
<input type="text" name="isbn" placeholder="Search..." />
<input class="myaccount" id="doSearch" name="doSearch" type="submit" value="Search" />
</form>

问题是,当用户点击搜索按钮或点击回车时,我需要清除输入文本搜索字段。按回车键和搜索按钮都会执行搜索,因此在任何一个操作发生时都必须清除。

但是,我完全被难住了。

任何帮助将不胜感激!!

编辑:我已经尝试过 Treffynnon 的建议。问题是,我已禁用搜索按钮实际加载 ajax.php。因此,当我使用该代码时,它会阻止页面停留在 isbn.php 上并加载 ajax.php 。这是加载搜索结果的代码。

isbn.php:
<script>
// attach a submit handler to the form
$("#searchForm").submit(function(event) {
// stop form from submitting normally
event.preventDefault();

// get some values from elements on the page:
var $form = $( this ),
term = $form.find( 'input[name="isbn"]' ).val(),
//term = "isbn",
url = $form.attr( 'action' );
$.post( url, { doSearch: "Search", isbn: term } ,
function( data ) {
var content = $( data );

$( "#result" ).append( content );

}
);
});
</script>

最佳答案

$('#searchForm').submit(function(){
// do search loading code here and then
$('input[name="isbn"]').val('');
});

根据您的更新试试这个:

// attach a submit handler to the form
$("#searchForm").submit(function(event) {
// stop form from submitting normally
event.preventDefault();

// get some values from elements on the page:
var $form = $( this ),
$term_input = $form.find('input[name="isbn"]'),
term = $term_input.val(),
//term = "isbn",
url = $form.attr( 'action' );
$.post( url, { doSearch: "Search", isbn: term } ,
function( data ) {
var content = $( data );

$( "#result" ).append( content );
}
);
$term_input.val('');
});

关于php - 清除输入或搜索按钮上的输入文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5500982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com