【问题标题】:How to remove text input from form field when clicked?单击时如何从表单字段中删除文本输入?
【发布时间】:2014-02-04 19:42:43
【问题描述】:

我有一组单选按钮和一个文本输入字段。当我单击文本输入时,使用 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>

【问题讨论】:

    标签: javascript jquery html forms


    【解决方案1】:

    只需使用val() 删除它们的值。 input 不是属性,是标签名。

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        试试这个,

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-12
          • 1970-01-01
          • 2015-09-21
          • 1970-01-01
          • 2011-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多