jquery If selector changes reset or clear text input field issue

时间:2015-06-25 18:42:17

标签: javascript jquery html

So, I want certain fields to reset or clear whenever the previous selector changes. I can get select tags to go back to the default option but can't get text input fields to clear out. Here is the jsfiddle to give you an idea of what I'm talking about: http://jsfiddle.net/wsk3opbf/ Below is my code: HTML <select id="type"> <option>--Select--</option> <option>45ppm</option> <option>55ppm</option> </select> <input id="testone" type="text"> </input> <select name="mediumcopier" id="mediumcopier"> <option value="">--Select--</option> <option value="">no</option> <option value="onecopier">yes</option> </select> <select name="largecopier" id="largecopier"> <option value="">--Select--</option> <option value="">no</option> <option value="onecopier">yes</option> </select> jquery $('#type').change(function(){ $('#smallcopier, #mediumcopier').prop('selectedIndex',0); $('#testone'.val() = ''); });

5 个答案:

答案 0 :(得分:3)

You don't have anything with an id smallcopier, and the set for testone was incorrect, try $('#type').change(function(){ $('#mediumcopier, #largecopier').prop('selectedIndex',0); $('#testone').val(''); });

答案 1 :(得分:2)

You provide new value as an parameter/argument to the val() function like below $('#type').change(function(){ $('#smallcopier, #mediumcopier').prop('selectedIndex',0); $('#testone').val(''); });

答案 2 :(得分:2)

Actually, .val() just returns a value that you can store into a variable. If you want to change the actual value of the selector, you have to pass it as a parameter. $('#testone').val('');

答案 3 :(得分:2)

It should be $('#testone').val('') ; not you can't assign like this $('#testone').val = ''; because val is method or function of jQuery object where parameters are passed as ('') or ('any value'), it's not variable to assign

答案 4 :(得分:1)

Your code: $('#testone'.val() = ''); Can't work, this is a syntax error; you have a misplaced ), it should be – syntactically, but still 'broken' – as follows: $('#testone').val() = ''; This, however, will give an error of Invalid left side in assignment (or similar) because you're trying to assign an empty string to another string. What I suspect you want is: $('#testone').val(''); Which will set the value of the #testone element to the empty string, or, possibly, to set the <input> back to the default value: $('#testone').val(function () { return this.defaultValue; });