带按钮的表格,用于删除括号内的括号和文字

时间:2017-02-10 02:29:18

标签: javascript

我想知道如何设置此功能以在输入表格中定位文字' 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..." />

请帮助我理解这应该如何运作!非常感谢!

1 个答案:

答案 0 :(得分:0)

document.getElementById('STOCK1')传递给removeBrackets来电,设置.value element而非设置document.body.innerHTML。请注意,您也可以使用RegExp /\[.*\]/g

&#13;
&#13;
<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;
&#13;
&#13;