简体   繁体   中英

Set a reset button to clear a specific single input of type text

I have the following:

<div id="section1">
 <label> Label 1 </label>
 <br>
 <input type="text" id="input_section1">
 <button type="reset" class="clear"></button>
</div>

This is how the html must remain structured. I have other divs on the page that are the same as the above (called #section2, #section3 etc), and none of the inputs are within a form.

So what I want is the button to reset the specific input it is associated with; in the case above, the input '#input_section1'.

At the moment when I click the reset button it is clearing every input on the page.

a simple solution:

<input type="button" onClick="document.getElementById('input_secion1').value='';">

This should do what you are asking for.

You can use the following jquery code using .siblings() :

$(document).on('click','.clear',function(){
    $(this).siblings( "input" ).val('');
});

This will find the input type sibling of the current clicked element and reset it.

See the fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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