简体   繁体   English

jQuery如果选择器更改重置或清除文本输入字段问题

[英]jquery If selector changes reset or clear text input field issue

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/ 这是jsfiddle,可让您大致了解我在说什么: http : //jsfiddle.net/wsk3opbf/

Below is my code: HTML 下面是我的代码: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 jQuery的

$('#type').change(function(){
    $('#smallcopier, #mediumcopier').prop('selectedIndex',0);
    $('#testone'.val() = '');
});

You don't have anything with an id smallcopier, and the set for testone was incorrect, try 您没有带id smallcopier的东西,并且testone的设置不正确,请尝试

$('#type').change(function(){
    $('#mediumcopier, #largecopier').prop('selectedIndex',0);
    $('#testone').val('');
});

You provide new value as an parameter/argument to the val() function like below 您将新值作为val()函数的参数/参数提供,如下所示

$('#type').change(function(){
    $('#smallcopier, #mediumcopier').prop('selectedIndex',0);
    $('#testone').val('');
});

Actually, .val() just returns a value that you can store into a variable. 实际上, .val()只是返回一个可以存储到变量中的值。 If you want to change the actual value of the selector, you have to pass it as a parameter. 如果要更改选择器的实际值,则必须将其作为参数传递。

$('#testone').val('');

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 因为val是jQuery对象的方法或函数,其中参数以('')或('any value')传递,所以赋值不是变量

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. 但是,这将Invalid left side in assignment (或类似内容) Invalid left side in assignment的错误,因为您试图将一个空字符串分配给另一个字符串。 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元素的值设置为空字符串, 或者可能将<input>设置回默认值:

$('#testone').val(function () {
    return this.defaultValue;
});

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

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