简体   繁体   English

在提交按钮上清除文本框,而无需重新加载页面

[英]clear textbox on submit button without reloading the page

Below is the jquery and the two code lines one for the text box and the other for the submit button but i just cant get them to link. 下面是jQuery和两个代码行,一个用于文本框,另一个用于提交按钮,但是我无法让它们链接。 I have tried a few different ways suggested on here but i cannot get the submit button to link to the text box and clear it. 我尝试了一些建议的其他方法,但是我无法使“提交”按钮链接到文本框并清除它。 All help greatly appreciated 所有帮助表示赞赏

$j is set as noConflict() $j设置为noConflict()

$j('.submit').keypress(function(){
    $j('.comment').val('');
});
<textarea name="name" rows="5" class="fullinput" id="comment"></textarea> 
<input id="submit" type="submit" value="Add to List" class="add" />

UPDATE - So i realised that i was using the incorrect selectors the correct code should have been. 更新-所以我意识到我使用了错误的选择器,正确的代码应该是正确的。

$j('#submit').click(function(event){
  $j('#comment').val('');
  event.preventDefault();
});

This along with the answers below resolved this issue. 这与下面的答案一起解决了此问题。

This should work: 这应该工作:

$j('#submit').click(function(event){
    $j('#comment').val('');
    event.preventDefault();
});

By clicking on a "submit" input, the user is invoking a postback. 通过单击“提交”输入,用户正在调用回发。 Therefore you need to use the built-in javascript event to prevent the postback from happening using the .preventDefault() method. 因此,您需要使用内置的javascript事件,以防止使用.preventDefault()方法发生回发。

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

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