简体   繁体   English

jquery不改变文本输入的值

[英]jquery not changing value of text input

I'm trying to create a "How did you find us form element" and I'm having some jQuery trouble. 我正在尝试创建一个“你是如何找到我们的表单元素”而且我遇到了一些jQuery问题。 The user selects from a list of options, one of which is "other". 用户从选项列表中进行选择,其中一个是“其他”。 When selected other a text box that allows them to be more specific. 当选择其他文本框时,它们可以更具体。 In an effort to make this more user friendly that input is hidden when another option is displayed. 为了使用户更友好,在显示其他选项时隐藏输入。 I've got the jQuery working to show and hide the text input as the user changes the option but I would like it to clear any text in the text box in the event the user selects other, fills something in, then selects another option. 我已经让jQuery工作以显示和隐藏文本输入,因为用户更改了选项,但我希望它清除文本框中的任何文本,如果用户选择其他,填充内容,然后选择另一个选项。

<label for="pcFindUs">How did you hear about us?</label>
        <select name="pcFindUs" id="pcFindUs" onChange="getval();">
          <option value="No Answer">Select One</option>
          <option value="Internet Search">Internet search</option>
          <option value="Internet Advertisement">Internet ad</option>
          <option value="Soclail Media">Social media </option>
          <option value="Unknown">I don't remember</option>
          <option value="other">Other</option>
        </select><br/>
        <div id="pcHiddenOtherSpecify" style="display:none;">
          <label for="pcFindUsSpecify">(Please Specify): </label><input type="text" value="" id="pcFindUsSpecify" name="pcFindUsSpecify" maxlength="50">
        </div>
        <script>
          function getval(){
            var values = $('#pcFindUs :selected').val();
            if (values == "other"){
              $("#pcHiddenOtherSpecify").css("display","block");
            }else{
              $("#pcHiddenOtherSpecify").attr("value","");
              $("#pcHiddenOtherSpecify").css("display","none");
            }
          }
        </script>

The pcHiddenOtherSpecify div containing the additional input appears and disappears just fine, but the value of #pcHiddenOtherSpecify still has whatever the user entered. 包含附加输入的pcHiddenOtherSpecify div显示并消失,但#pcHiddenOtherSpecify的值仍然具有用户输入的内容。 I've also tried 我也试过了

$("#pcHiddenOtherSpecify").val("");

With no luck. 没有运气。 Any ideas what I may be doing wrong here? 我在这里做错了什么想法?

You are trying to change the value of a div element, not an input. 您正在尝试更改div元素的值,而不是输入。 Try this: 尝试这个:

$("#pcFindUsSpecify").val("");

错误的身份证

$("#pcFindUsSpecify").val("");

try $("#pcFindUsSpecify").val(""); 尝试$("#pcFindUsSpecify").val("");

check it out http://codepen.io/JcBurleson/pen/MKBBWq 看看http://codepen.io/JcBurleson/pen/MKBBWq

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

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