Clear button Settings on a form

2019-08-17 08:24发布

I am using a form and was wondering if anyone knows some JavaScript that can edit what fields the clear button actually clears?

i.e i only want a select number of fields to be cleared once the user hits the clear button on the form.

input id='FrmReset' type='reset' value='Clear' name='resetButton' onclick='formReset(document.getElementById("Form")); return false;'  

I just have the following which clears everything

<input id='FrmReset' type='reset' value='Clear' name='resetButton' onclick='formReset(document.getElementById("Form")); return false;' />

1条回答
冷血范
2楼-- · 2019-08-17 08:54

Let's say you have 4 fields like this:

<input type="text" value="don't clear this" id="i1" />
<input type="text" value="don't clear this" id="i2" />
<br/>
<input type="text" value="clear this" id="i3" />
<input type="text" value="clear this" id="i4" />

And you need to clear only bottom 2. Add a normal button with onclick event:

<button onclick="clearNeeded()">Clear only bottom fields</button>

function clearNeeded() {
    document.getElementById('i3').value='';
    document.getElementById('i4').value='';
}

As you can see - the function selectively picks only specific fields and resets their values.

Demo: http://jsfiddle.net/rdnT3/

查看更多
登录 后发表回答