单击时如何从表单域中删除文本输入?

时间:2014-02-04 19:42:43

标签: javascript jquery html forms

我有一组单选按钮和一个文本输入字段。当我单击文本输入时,使用JavaScript取消选中单选按钮,但是当我在表单字段中键入内容然后重新选择单选按钮时,我无法删除要删除的文本。这是我的代码到目前为止,如果有人可以帮助我让文本框清除所有输入,当我选择一个我知道即将关闭的收音机。

<input type="radio" id="radio1" name="radios"   checked>
<label for="radio1">$10</label>
<input type="radio" id="radio2" name="radios" >
<label for="radio2">$25</label>
<input type="radio" id="radio3" name="radios"  >
<label for="radio3">$50</label>
<input type="radio" id="radio4" name="radios" >
<label for="radio4">$100</label>
<input type="text" id="textInput">
<script>
<!-- to remove radio on text input click-->
$('#textInput').click(function () {
    $.each($('input[type=radio]'), function () {
         $(this).removeAttr("checked");
});
});
<!-- to remove text on text radio button click-->
$('input[type=radio]').click(function () {
    $.each($('#textInput'), function () {
        $(this).removeAttr("input");
});
});
</script>

3 个答案:

答案 0 :(得分:3)

只需使用val()删除其值即可。 input不是属性,而是标记名称。

$.each($('#textInput'), function () {
    $(this).val("");

答案 1 :(得分:2)

只需使用 .val(),您也不需要$.each

$('input[type=radio]').click(function () {
    $('#textInput').val('');            
});

答案 2 :(得分:2)

您必须使用.val("")将文本框值设置为空。

试试这个,

$('#textInput').each(function () {
   $(this).val("");
});
相关问题