The
Browser support
All major browsers support the tag.
Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between and , while other browsers will submit the content of the value attribute. Please use input elements in HTML forms to create buttons.
Notes
When using the tag, it is easy to take it for granted and use it as , which can easily lead to the following: Wrong usage:
1. Get the value ofbuttonvalue through $('#customBtn').val()
In IE When used in this way under (IE kernel), the value obtained is "button", not "test". Under non-IE, the value obtained is "test". Attend the first sentence marked in red above.
This should be distinguished from .
Through these two methods, $('#customBtn').val(), $('#customBtn').attr('value') obtains the value in different browsers, as follows:
You can verify this by testing the following code
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<scripttype="text/javascript"src="jquery-1.4.4.min.js"></script>
<scripttype="text/javascript">
$(function(){
$('#test1').click(function(){
alert($('#customBtn').attr('value'));
});
$('#test2').click(function(){
alert($('#customBtn').val());
});
});
</script>
</head>
<body>
<buttonid="customBtn"value="test">按钮</button>
<inputtype="button"id="test1"value="getattr"/>
<inputtype="button"id="test2"value="getval"/>
</body>
</html>
2. Inadvertently put the tag into the
<html>
<body>
<formaction="">
<button>button</button>
<inputtype="submit"value="inputsubmit"/>
<inputtype="button"value="inputbutton"/>
</form>
</body>
</html>
The above is the detailed content of The difference and precautions between button and input type=button. For more information, please follow other related articles on the PHP Chinese website!