如何使用JQuery重置单个输入元素

时间:2015-09-02 08:26:27

标签: jquery html-input

如何使用JQuery重置单个输入元素(是JQuery还是JavaScript)?

How to reset a form using jQuery with .reset() method显示了如何重置整个表单,但我想重置单个元素,而不是整个表单。

6 个答案:

答案 0 :(得分:3)

您可以使用defaultValue属性访问input元素的原始值。

例如,你可以这样做:

var myInput = document.getElementById("myInput");

myInput.value = myInput.defaultValue;

答案 1 :(得分:2)

您可能需要使用hack来实现此目的。

使用.wrap()临时用表格包装所需的输入。

$('#input1,#input2').val('testUpdated');
$('#input1').wrap('<form/>');
$('#input1').closest('form')[0].reset();
$('#input1').unwrap();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <input id="input1" value="test"/>
    <input id="input2" value="test"/>
</form>

答案 2 :(得分:1)

如果已在input元素中设置了值,则可以将其引用回

&#13;
&#13;
 $('#resetName').click(function () {
     $('#name').val($('#name').attr('value'));
 });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="TEST" id="name"></input>
    <input value="99" id="age"></input>
    <input value="1 Apr 2000" id="DOB"></input>
    
    <button id="resetName" value="Reset Name">Reset Name</button>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

使用jQuery重置单个输入字段:

import cv2
import numpy as np

image1 = cv2.imread( 'E:\\alada.jpg',  0 )
image2 = cv2.imread( 'E:\\alada2.jpg', 0 )
dest   = 'E:\\Ratio\\'
print image1.shape

image1 = cv2.resize( image1, ( 300, 200 ) )
image2 = cv2.resize( image2, ( 300, 200 ) )
img1   = image1.ravel()
img2   = image2.ravel()
sd1    = np.std( img1 )
sd2    = np.std( img2 )
img2   = ( ( img2 - np.mean( img2 ) ) * sd1 / sd2 ) + np.mean( img1 )
ratio  = img1 / img2
ratio  = np.arctan(  ratio ) - np.pi / 4
ratio  = np.reshape( ratio, ( image1.shape[0], image1.shape[1] ) )
print ratio.shape

thresh, th2 = cv2.threshold( ratio, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU )


error: ..\..\..\..\opencv\modules\imgproc\src\thresh.cpp:718: error: (-215) 
src.type() == CV_8UC1 in function cv::threshold

答案 4 :(得分:1)

这很简单

$('#escape').on('click', function(e){
                        $('#field').val($('#field').defaultValue);
});

答案 5 :(得分:-1)

FIDDLE

尝试使用$(this).val(defaultVal);作为

('#maincontainer').find('textarea,input[type=text]').each(function () {
        var defaultVal = $(this).data('default');
        $(this).val(defaultVal);
    });

如果要在此处清除特定输入,请更改ID我只是说明如何清除输入和文本区域