我想知道如何设置此功能以在输入表格中定位文字' STOCK1'。
Javascript直播:
function removeBrackets(element) {
document.body.innerHTML = element.replace(/\[[^\]]+\]/g, '__');
}
HTML现状:
<button onclick="javascript:removeBrackets(element)">Remove Brackets and Text Inside Brackets</button>
<input type="text" name="STOCK1" id="STOCK1" placeholder="Place text here..." />
请帮助我理解这应该如何运作!非常感谢!
答案 0 :(得分:0)
将document.getElementById('STOCK1')
传递给removeBrackets
来电,设置.value
element
而非设置document.body.innerHTML
。请注意,您也可以使用RegExp
/\[.*\]/g
。
<script>
function removeBrackets(element) {
element.value = element.value.replace(/\[.*\]/g, '__');
}
</script>
<button onclick="removeBrackets(document.getElementById('STOCK1'))">Remove Brackets and Text Inside Brackets</button>
<input type="text" name="STOCK1" id="STOCK1" placeholder="Place text here..." />
&#13;